我应该创建一个程序,使用用户的输入创建平均测试分数。我还需要确保当用户说“结束”而不是输入分数时,程序会有中断并且会给用户提供结果。但是,我无法使程序正常运行并想要一些输入。
#!/usr/bin/env python3
#display a welcome message
print("The test Scores program")
print()
print("Enter end to stop input") #change from 999 to end
print("========================")
print()
#variables
counter = 0
score_total = 0
test_score = 0
choice = "y"
while choice.lower():
while True:
test_score =input("Enter test score: ")
if test_score == "end":
break
elif (test_score >= 0) and (test_score <= 100):
score_total += test_score
counter += 1
else:
print("Test score must be from 0 through 100. Try again>")
#calculate average score
average_score = round(score_total / counter)
#display result
print("=======================")
print("Total Score: ", score_total)
print("Average Score: ", average_score)
print()
#see if user wants to continue
choice = input("Continue (y/n)? ")
print()
print("Bye")
答案 0 :(得分:2)
当您执行(test_score >= 0) and (test_score <= 100)
时,您正在比较字符串和int,当您将输入与数字进行比较时,您希望将输入转换为int。
试试这个:
test_score =input("Enter test score: ")
if test_score == "end":
break
elif (int(test_score) >= 0) and (int(test_score) <= 100):
score_total += int(test_score)
counter += 1
else:
print("Test score must be from 0 through 100. Try again>")
我只是在将test_score与数字进行比较时将其转换为int。
答案 1 :(得分:0)
已经很晚了,但这是给碰巧遇到相同问题的任何人的。您可以尝试从Test_score = input(“ Enter test score:”):)中分离出'int'
while True:
test_score = input("Enter test score: ")
if test_score == 'end':
break
test_score = int(test_score)
if 0 <= test_score <= 100:
score_total += test_score
counter += 1
else:
print(
"Test score must be from 0 through 100. Score discarded. Try again."
)