Reputation: 5
I am trying to implement input validation in the form on a while loop. When I enter a negative number for the midterm grade the program don't ask again. I've tried replacing the or with an and in the while again line but that still doesn't change the outcome. The input validation works for the while assessments line but not on the while again line. I believe the issue is with this piece of the code.
Edit: When I enter a character other than N or Y the program prints the grade. The program should ask the user what grade the got on the midterm 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()
The whole code is posted below for context.
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()
Upvotes: 0
Views: 74
Reputation: 3782
Because you're using the or
conditional, the loop will repeat unless again
is equal to "Y" and is equal to "N", which is impossible. Using the and
conditional should solve the problem:
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()
The loop will now exit if again
does not equal "Y" and again
does not equal "N", which is what you want.
Upvotes: 2