2016-03-06 54 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 ...' –