我正在尝试以while循环的形式实现输入验证。当我为期中成绩输入负数时,程序不再询问。我试过用and在while线上再次替换or,但这仍然不会改变结果。输入验证适用于while评估行,但不适用于while再次行。我相信问题在于这段代码。
编辑: 当我输入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()
答案 0 :(得分:2)
因为您使用的是or
条件,否则循环将重复,除非again
等于“ Y” 等于“ N”,这是不可能的。使用and
条件应该可以解决问题:
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()
如果again
不等于“ Y” again
不等于“ N”,那么循环将退出。