首页 > 解决方案 > 如何在 Python 中打印来自 while 循环和 if else 的变量

问题描述

我从程序中得到的错误是未定义的变量。

*

# Python 3.9.4
# import sys
# print(sys.version)

SubjectT = input("Enter your subject: ")
Tutor_Name = input("Enter your name: ")
Iterate = int(input("How many students are there? "))
# Set counter
# create count for each student
Accumulate = 0
for i in range(Iterate):  # It will run how many time based on Iterate input
    Score = float(input("Enter score: "))
    Counter = True
    while Counter:
        if 80 <= Score < 100:
            Accumulate1 = Accumulate + 1
        elif 80 > Score >= 65:
            Accumulate2 = Accumulate + 1
        elif 65 > Score >= 50:
            Accumulate3 = Accumulate + 1
        elif Score < 50:
            Accumulate4 = Accumulate + 1
        else:
            print("The number is a negative value or incorrect")
            Again = input("Enter yes for restarting again or if you want to exit the program please press anything: ")
            if Again in ("y", "Y", "YES", "Yes", "yes"):
                continue
            else:
                break
print(f"Subject title:", {SubjectT}, "\nTutor:", {Tutor_Name})
print("Grade", "     Number of students")
print("A", "Students: ", Accumulate1)
print("B", "Students: ", Accumulate2)
print("C", "Students: ", Accumulate3)
print("D", "Students: ", Accumulate4)

这是我在 Stackoverflow 上的第一篇文章。请原谅我的任何不当内容。非常感谢。

标签: pythonif-statementwhile-loopprintingcount

解决方案


你说

Counter = True 

没有迭代,因此它将无限期地运行。使用 while 循环,您需要设置一些约束。


推荐阅读