2 回答

TA貢獻2019條經(jīng)驗 獲得超9個贊
對 Python 很陌生,我正試圖為我的經(jīng)理制作一個小費計算器。
小費的計算方式是服務(wù)器工作的那一天的百分比,他們會得到該百分比的小費。例如,如果服務(wù)器 1 當天有 30% 的時間工作,則他們會收到當天提供的 30% 的小費。
我一直在試圖弄清楚如何將一個數(shù)字(代碼中的變量“hourNum”)除以存儲服務(wù)器工作時間的數(shù)組中的每個元素。這將使我獲得服務(wù)器工作當天的百分比。
使用下面的代碼,數(shù)學似乎無法正確計算也無法正確打印。
不幸的是,我無法在 Stack Overflow 上找到任何類似的問題。
下面附上源代碼,問題出在標題為“計算服務(wù)器工作日百分比”的最后一節(jié)中:
#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條經(jīng)驗 獲得超6個贊
將您的最后一段代碼更改為:
#calculate percent of day servers worked
for i in lst:
print(f"Worker {i+1}: {round(i/sum(lst)*100)}%")
添加回答
舉報