测试分数程序- Python

qgelzfjb  于 2023-02-07  发布在  Python
关注(0)|答案(3)|浏览(105)

我应该创建一个程序,将创建一个平均测试分数使用用户的输入。我还需要确保当用户说'结束',而不是输入一个分数,该程序与休息,并会给予用户的结果。但是,我不能让程序正确运行,并希望一些输入。

#!/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")
mm5n2pyu

mm5n2pyu1#

当你做(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。

5gfr0r5j

5gfr0r5j2#

现在已经很晚了,但这是给任何碰巧有同样问题的人的。你可以尝试将'int'与Test_score = input(“Enter test score:“):)分开

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."
            )
0sgqnhkj

0sgqnhkj3#

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>")

相关问题