2016-03-06 61 views
-1

所以我有這個計劃它在哪裏打印所有用戶的數據,按照他們輸入內容的被輸入並給出了相應的信息:如果報表打印錯誤

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 
+1

的索引。 .. –

+0

有問題的代碼與輸出不匹配! 'hourlist'將爲空,導致'IndexError:列表索引超出範圍'。 –

+0

對不起,我忘了這部分hourslist.append(小時) – Hassan

回答

0

你在代碼中存在的一個關鍵問題是你不使用hourslist。

9號線和10號線之間,你應該添加hourslist.append(小時)

,並在if語句你沒有添加您未使用hourslist hourslist

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[x] >=10 and hourslist[x] <= 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" 
+0

是的,我忘了添加該部分,但即使與它,我有相同的錯誤 – Hassan

+0

是的,工作謝謝 – Hassan

0

你不更新小時的循環,所以小時將是你最後輸入的號碼。

+0

謝謝,那麼我如何更新它? – Hassan

+0

是這樣的? 'hours = hourslist [x]' –

+0

或者簡單地在你的「ifs」中使用hourslist [x]如果如果hourslist [x]> = 10 ...' –