【问题标题】:How can I print variable which is int from while loop and if else in Python如何在 Python 中打印来自 while 循环和 if else 的变量
【发布时间】:2021-04-24 14:00:16
【问题描述】:

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

*

# 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 上的第一篇文章。 请原谅我的任何不当内容。 非常感谢。

【问题讨论】:

  • if-elif 语句正在声明变量,这些变量可能并不总是被执行,并且您最终希望声明所有 Accumulate 1,2,3,从而导致所述错误。

标签: python if-statement while-loop printing count


【解决方案1】:

你说

Counter = True 

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

【讨论】:

    【解决方案2】:

    我意识到为什么你有 While 循环,但它仍然不能有效地为我工作 - 如果所有答案都在范围内,它永远不会终止。这应该对你有用,它对我有用。我对其进行了更改,因此每次有人输入分数时它都会检查它是否在正确的范围内,因此如果值不正确,您不必重复整个循环

    SubjectT = input("Enter your subject: ")
    Tutor_Name = input("Enter your name: ")
    NoOfStudents = int(input("How many students are there? "))
    # Set counter
    # create count for each student
    Accumulate = 0
    Accumulate1 = 0
    Accumulate2 = 0
    Accumulate3 = 0
    Accumulate4 = 0
    
    def validRange(score):
        if 0 <= score <=100:
            return True
        else:
            return False
    i = 1
    while (i <= NoOfStudents):  # It will run how many time based on Iterate  
    
    validInput = False
    while (not validInput):
        Score = float(input("Enter score: "))
        if ( not validRange(Score)):
            print("The number is a negative value or incorrect")
        else:
            validInput = True
            if 80 <= Score < 100:
                Accumulate1 = Accumulate1 + 1
            elif 80 > Score >= 65:
                Accumulate2 = Accumulate2 + 1
            elif 65 > Score >= 50:
                Accumulate3 = Accumulate3 + 1
            elif Score < 50:
                Accumulate4 = Accumulate4 + 1
            i = i+1
        
    
    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)
    

    【讨论】:

      猜你喜欢
      • 2018-01-13
      • 2023-03-22
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多