如果报表打印错误

时间:2016-03-06 16:03:38

标签: python

所以我有这个程序,它打印输入的所有用户数据,并根据他们输入的内容给出相应的消息:

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,它总是打印&#34;你没有空闲时间&#34;。当只有一个人时,不会发生此问题。我已经尝试了很多方法来尝试修复此问题,但没有任何效果,谢谢。

这是我得到的:

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

2 个答案:

答案 0 :(得分:0)

您没有在for循环中更新小时,因此小时将是您的最后一个输入数字。

答案 1 :(得分:0)

您在代码中遇到的一个关键问题是您不使用小时列表。

在第9行和第10行之间,您应该添加hourslist.append(小时)

并在 if语句中您没有添加小时列表索引

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"