所以我有這個計劃它在哪裏打印所有用戶的數據,按照他們輸入內容的被輸入並給出了相應的信息:如果報表打印錯誤
nameslist = []
hourslist = []
morestudents = True
while morestudents:
name = raw_input("Enter name : ")
nameslist.append(name)
hours = int(input("enter hours booked : "))
hourslist.append(hours)
more = raw_input("add more students? (yes/no): ")
if more <> "Y" and more <> "y" and more <> "yes" and more <> "Yes":
morestudents = False
print
for x in range (len(nameslist)):
print
print nameslist[x],"You have", hourslist[x], "hours in total"
print
if hourslist >=10 and hourslist <= 14:
print "You have 1 free hour"
elif hourslist >= 15:
print "You have 2 free hours"
elif hourslist <= 9:
print "You have no free hours"
nameslist是所有名的數組輸入並且小時是用戶輸入的整數。無論如何,當我有不止一個人進入他們的時間時,即使他們的時間大於10,它總是打印出「你沒有空閒時間」。只有一個人時不會發生此問題。我已經嘗試了很多方法來嘗試解決這個問題,但沒有任何工作,謝謝。
這裏是我所得到的:
Enter name : Jack Smith
enter hours booked : 21
add more students? (yes/no): y
Enter name : John Wayne
enter hours booked : 2
add more students? (yes/no): n
Jack Smith You have 23 hours in total
You have no free hours
John Wayne You have 2 hours in total
You have no free hours
的索引。 .. –
有問題的代碼與輸出不匹配! 'hourlist'將爲空,導致'IndexError:列表索引超出範圍'。 –
對不起,我忘了這部分hourslist.append(小時) – Hassan