Python Forum
Help? Letter Grade assignment.. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Help? Letter Grade assignment.. (/thread-2981.html)



Help? Letter Grade assignment.. - zepel - Apr-22-2017

I need an error message to display to the user if they try to enter a score over 100 and I need to use the sys.exit() to exit the program gracefully if any errors are detected. I need to do this in an if statement. I can't figure this out, I'm a total noob. Help please..


name = input("Please enter your First and Last Name: ")
score = int(input("Enter your first exam score: "))
score2 = int(input("Enter your second exam score: "))
score3 = int(input("Enter your third exam score: "))

average = (score + score2 + score3) // 3

if average >= 100:
    print("Error")
    sys.exit()
elif average >= 90 and average <= 100:
    print("You received an A.")
elif average >= 80 and average < 90:
    print("You received a B.")
elif average >= 70 and average < 80:
    print("You received a C.")
elif average >= 60 and average < 70:
    print("You received a D.")
else:
    print("You received an F.")
        
print("Report Card")
print("Name:", name)
print("You scored a", score, "on your first exam.")
print("You scored a", score2, "on your second exam.")
print("You scored a", score3, "on your third exam.")
print("Your average score is" ,average,"percent.")



RE: Help? Letter Grade assignment.. - micseydel - Apr-22-2017

What's wrong with what you have? (Other than probably wanting > 100 instead of >= 100.)


RE: Help? Letter Grade assignment.. - smbx33 - Apr-23-2017

You could put in if statement after every variable that will ask for same input IF input does not meet the criteria, in this case its a number being over 100.

score = int(input("Enter your first exam score: "))
if score > 100:
   print("score can not be over 100!")
   score = int(input("Enter your first exam score: "))

score2 = int(input("Enter your second exam score: "))
if score2 > 100:
   print("score can not be over 100!")
   score2 = int(input("Enter your second exam score: "))

score3 = int(input("Enter your third exam score: "))
if score3 > 100:
   print("score can not be over 100!")
   score3 = int(input("Enter your third exam score: "))



RE: Help? Letter Grade assignment.. - idontreallywolf - Apr-23-2017

if average >= 100: this means if average is HIGHER THAN or EQUALS TO 100
What you should use here is just > instead of >=