对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
任何提示或帮助将不胜感激:)
表达式hourNum / lst[i]
应为100 * lst[i] / hourNum
:该值表示每个服务器工作的工作日的小时数百分比(hourNum),再乘以100,以百分比表示。另外,为什么不打印最终百分比,为什么不将它们存储在列表中,以便程序记住它们呢?
#get hours in the day
hourNum = int(input('Enter how many hours were worked in the day: '))
#get tips for the day
tipNum = int(input("Enter how much tips were earned (enter to the nearest whole dollar, do not use a dollar sign): "))
# 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
lst_percents = []
for i in range (0,serverNum):
lst_percents.append(round(100* lst[i] / hourNum))
print("The percent of day the servers worked:", lst_percents)
样本输入和输出:
Enter how many hours were worked in the day: 10
Enter how much tips were earned (enter to the nearest whole dollar, do not use a dollar sign): 100
Enter number of servers that worked the day: 4
Enter the number of hours each server worked (in order):
1
2
4
5
You entered: [1, 2, 4, 5]
The percent of day the servers worked: [10, 20, 40, 50]
我将把有关每个服务器的提示的计算留给您。希望通过列出服务器工作天数的百分比会更容易。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句