2 回答
TA贡献2019条经验 获得超9个赞
对 Python 很陌生,我正试图为我的经理制作一个小费计算器。
小费的计算方式是服务器工作的那一天的百分比,他们会得到该百分比的小费。例如,如果服务器 1 当天有 30% 的时间工作,则他们会收到当天提供的 30% 的小费。
我一直在试图弄清楚如何将一个数字(代码中的变量“hourNum”)除以存储服务器工作时间的数组中的每个元素。这将使我获得服务器工作当天的百分比。
使用下面的代码,数学似乎无法正确计算也无法正确打印。
不幸的是,我无法在 Stack Overflow 上找到任何类似的问题。
下面附上源代码,问题出在标题为“计算服务器工作日百分比”的最后一节中:
#get hours in the day
print('Enter how many hours were worked in the day: ')
hourNum = int(input())
#get tips for the day
print("Enter how much tips were earned (enter to the nearest whole dollar, do not use a dollar sign): ")
tipNum = int(input())
# creating an empty list
lst = []
# number of elemetns as input
serverNum = int(input("Enter number of servers that worked the day : "))
# iterating till the range
print("Enter the number of hours each server worked (in order): ")
for i in range(0, serverNum):
ele = int(input())
lst.append(ele) # adding the element
print("You entered: ", lst)
#calculate percent of day servers worked
n = 0
for i in range (0,serverNum):
print (hourNum / lst[0 + n])
n+1
任何提示或帮助将不胜感激:)
TA贡献1816条经验 获得超6个赞
将您的最后一段代码更改为:
#calculate percent of day servers worked
for i in lst:
print(f"Worker {i+1}: {round(i/sum(lst)*100)}%")
添加回答
举报