Python 满足条件时循环未运行

Python 满足条件时循环未运行,python,python-3.x,loops,while-loop,Python,Python 3.x,Loops,While Loop,我试图在while循环的表单中实现输入验证。当我为期中成绩输入一个负数时,程序就不会再问了。我试着在while-reach行中用and替换or,但这仍然不会改变结果。输入验证适用于while评估行,但不适用于while-reach行。我相信问题在于这段代码 编辑: 当我输入N或Y以外的字符时,程序将打印等级。程序应该再次询问用户期中考试的分数 def again(): print ("again") again = 0 while again != "Y" or agai

我试图在while循环的表单中实现输入验证。当我为期中成绩输入一个负数时,程序就不会再问了。我试着在while-reach行中用and替换or,但这仍然不会改变结果。输入验证适用于while评估行,但不适用于while-reach行。我相信问题在于这段代码

编辑: 当我输入N或Y以外的字符时,程序将打印等级。程序应该再次询问用户期中考试的分数

def again():
    print ("again")
    again = 0
    while again != "Y" or again != "N":
        again = input("Do you want to calculate the grade again? Please enter either Y or N. ")
    if again == "Y":
        main()
整个代码发布在下面的上下文中

import sys
def main():
    #Get the name and # of assessments
    named = ("name")
    un = name(named)
    tp = 0
    s= 0
    assessments = 0
    while assessments <= 0:
        assessments = int(input("how many assessments have you completed?"))


    #Get the total of assessments including homework
    for x in range(1,assessments + 1):
        g = float(input("grade of assessment"))
        tp += g

    scoree = tp / assessments
    finalG= finals(scoree)
    scoring(finalG)
    p(un)
    again()


def scoring(score):

    if score >= 97:
        print("You have an A+ in the class!")
        print ("Your grade as a percentage is:", score, "%",sep="") #Print the percentage
        sys.exit()
    if score >= 93 and score <= 96.9:
        print("You have an A in the class!")
        print("Your grade as a percentage is:", score,"%",sep="")
        sys.exit()
    if score >= 90 and score <= 92.9:
        print("You have an A- in the class!")
        print("Your grade as a percentage is:", score,"%",sep="")
        sys.exit()
    if score >= 87 and score <= 89.9:
        print("You have an B+ in the class!")
        print("Your grade as a percentage is:", score,"%",sep="")
        sys.exit()
    if score >= 83 and score <= 86.9:
        print("You have an B in the class!")
        print("Your grade as a percentage is:", score,"%",sep="")
        sys.exit()
    if score >= 80 and score <= 82.9:
        print("You have an B= in the class!")
        print("Your grade as a percentage is:", score,"%",sep="")
        sys.exit()
    if score >= 77 and score <= 79.9:
        print("You have an C+ in the class!")
        print("Your grade as a percentage is:", score,"%",sep="")
        sys.exit()
    if score >= 73 and score <= 76.9:
        print("You have an C in the class!")
        print("Your grade as a percentage is:", score,"%",sep="")
        sys.exit()
    if score >= 70 and score <= 72.9:
        print("You have an C= in the class!")
        print("Your grade as a percentage is:", score,"%",sep="")
        sys.exit()
    if score >= 67 and score <= 69.9:
        print("You have an D+ in the class!")
        print("Your grade as a percentage is:", score,"%",sep="")
        sys.exit()
    if score >= 63 and score <= 66.9:
        print("You have an D in the class!")
        print("Your grade as a percentage is:", score,"%",sep="")
        sys.exit()
    elif score >= 60 and score <= 62.9:
        print("You have an D- in the class!")
        print("Your grade as a percentage is:", score,"%",sep="")
        sys.exit()
    else: 
        print("You have an F")
        print("Your grade as a percentage is:", score,"%",sep="")


#ask for their name with "input"
def name(names):
    names = input("Could you please tell me what is your name?:")
    return names

    #Print the grade of the student with the name of the student in the same function
def p(p2):
    print("is your current average is",p2)

# asks if the use wants to run the program again?
def again():
    print ("again")
    again = 0
    while again != "Y" or again != "N":
        again = input("Do you want to calculate the grade again? Please enter either Y or N. ")
    if again == "Y":
        main()
#ask the user their final grades and adds it to the calculation
def finals(fgrade):
    mtg = 0
    ma = input("Did you take your midterm? Like before please enter either Y or N")
    if ma == "Y":
        while mtg <= 0:
            mtg = float(input("what is the midterm grade you got back?"))
        fgrade = mtg * .2 + fgrade *.8
    return fgrade

    if ma == "N":
        return fgrade
main()
导入系统 def main(): #获取评估的名称和# 命名=(“名称”) un=名称(已命名) tp=0 s=0 评估=0 评估=97时: 打印(“你在班上得了A+!) 打印(“你的分数百分比为:”,分数,“%”,sep=“”)#打印百分比 sys.exit()
如果分数>=93,分数=90,分数=87,分数=83,分数=80,分数=77,分数=70,分数=67,分数=63,分数=60,分数,因为您使用的是
条件,循环将重复,除非
再次
等于“Y”和“N”,这是不可能的。使用
条件应该可以解决问题:

def again():
    print ("again")
    again = 0
    while again != "Y" and again != "N": #Notice the "and" rather than the "or"
        again = input("Do you want to calculate the grade again? Please enter either Y or N. ")
    if again == "Y":
        main()

如果
再次
不等于“Y”且
再次
不等于“N”,则循环现在将退出,这是您想要的。

因为您使用的是
条件,循环将重复,除非
再次
等于“Y”且等于“N”,这是不可能的。使用
条件应该可以解决问题:

def again():
    print ("again")
    again = 0
    while again != "Y" and again != "N": #Notice the "and" rather than the "or"
        again = input("Do you want to calculate the grade again? Please enter either Y or N. ")
    if again == "Y":
        main()

如果
再次
不等于“Y”,并且
再次
不等于“N”,则循环现在将退出,这是您想要的。

这不是一个大问题,但当您有一个函数只从stdin获取输入并返回它时,就没有实际意义了。只是让你更难理解代码…@pizzastaticvoidmain是的,这是因为除非你想再次计算分数,否则循环中没有出口。当你尝试输入一个负数作为期中分数时,问题就来了。这不是什么大问题,但当你有一个函数只从stdin获取输入并返回它时,拥有它没有真正的意义。只是让你更难遵循代码…@pizzastaticvoidmain是的,这是因为除非你想再次计算成绩,否则没有退出循环的机会。当你尝试为期中成绩输入负数时,问题就出现了。
再次不在(“Y”,“N”)
再次“是”还是再说一遍!=“N”:
这种特定类型的错误有名称吗?它经常出现在新手代码中。
再次出现,而不是(“Y”、“N”)
再次出现!=“是”还是再说一遍!=“N”:
这种特定类型的错误有名称吗?它经常出现在新手代码中。