我只会发布问题所在的部分,程序没有错误(除raw_input
问题外,所有代码都有效)
我使用search_function(1)
等进行了测试,但它确实有效。
但如果我在循环时执行此操作,则不会打印任何内容。 示例输出:
输入一个数字来打印特定的表格, 或停止退出:2输入一个号码 打印特定表,或停止退出: 2输入要打印的数字 表,或停止退出:1输入一个 打印特定表格的数字,或STOP 退出:输入要打印的数字 特定表,或停止退出:1 输入一个数字来打印特定的表格, 或停止退出:输入一个数字 打印特定表,或停止退出: STOP
def search_function(x):
if x == 1:
for student in students:
print "%-17s|%-10s|%-6s|%3s" % student.print_information()
print '\n'
if x == 2:
print "%-17s|%-10s|%s" %(header[0],header[1],header[4])
print "-" * 45
for student in students:
print "%-17s|%-10s|%s" %student.print_first()
print '\n'
print "Simple Analysis on favorite sports: "
# Printing all sports that are specified by students
for s in set(Student.sports): # class attribute
print s, Student.sports.count(s), round(((float(Student.sports.count(s)) / num_students) *100),1)
# Printing sports that are not picked
allsports = ['Basketball','Football','Other','Baseball','Handball','Soccer','Volleyball','I do not like sport']
for s in set(allsports) - set(Student.sports):
print s, 0, '0%'
choice_list = Student.sports
for choice in choice_list:
choice_dict[choice] = choice_dict.get(choice, 0) + 1
print max(choice_dict)
print min(choice_dict)
elif x == 3:
print "%-17|%-10s|%-16s|%s" %(header[0],header[1],header[5],header[6])
print "-" * 45
for student in students:
print "%-17s|%-10s|%-16s|%s" % student.print_second()
print '\n'
elif x == 4:
print "%-17s|%-10s|%s" %(header[0],header[1],header[7])
print "-" * 45
for student in students:
print "%-17s|%-10s|%s" %student.print_third()
print '\n'
elif x == 5:
print "%-17s|%-10s|%-15s|%s" %(header[0],header[1],header[8],header[9])
print "-" * 45
for student in students:
print "%-17s|%-10s|%-16s|%s" % student.print_fourth()
print '\n'
x = raw_input("Enter a number to print specific table, or STOP to quit: ")
while x != 'STOP':
search_function(x)
x = raw_input("Enter a number to print specific table, or STOP to quit: ")
答案 0 :(得分:2)
raw_input()
返回字符串,而您的代码需要整数。使用search_function(int(x))
或更改条件以与字符串进行比较。
答案 1 :(得分:1)
首先测试x == 'STOP'
,如果为True则测试break
,否则转换为int并调用search_function
:
while True:
x = raw_input("Enter a number to print specific table, or STOP to quit: ")
if x == 'STOP':
break
search_function(int(x))