-1

I can't get past the while statement:

letter_grade=input("What is your letter grade(please use capitlization): ")
while letter_grade!="A" or "B+" or "B" or "C+" or "C" or "D" or "F":
    print ("Enter a valid letter")
    letter_grade=input("What is your letter grade(please use capitlization): ")
scharette
  • 9,437
  • 8
  • 33
  • 67

2 Answers2

1

Try this:

letter_grade=input("What is your letter grade(please use capitlization): ")
while letter_grade not in ["A","B+","B","C+","C","D","F"]:
    print ("Enter a valid letter")
    letter_grade=input("What is your letter grade(please use capitlization): ")

Your current code doesn't work since you can't simplify boolean statements like you have. To make what you have work you would have to change it to:

letter_grade!="A" and letter_grade!="B+" and #etc.

where you have to test every letter against the variable.

gommb
  • 1,121
  • 1
  • 7
  • 21
  • Care to share what was wrong with his code? – everton Oct 30 '17 at 03:04
  • 1
    @everton `while letter_grade!="A" or "B+" or "B" or "C+" or "C" or "D" or "F"` here all the character/string represents True i.e. or "B+" or "B" or "C+" or "C" or "D" or "F" which was causing infinite while loop. – Mahesh Karia Oct 30 '17 at 03:06
1

You seem to not understand how logical statements work. Your line is defined as:

while letter_grade!="A" or "B+" or "B" or "C+" or "C" or "D" or "F":

This always returns true since it does not mean what you want it to be. You wanted to use something like that:

while letter_grade!="A" and letter_grade!="B+" and letter_grade!="B": #etc

But for readability, I would recommend that code :

letter_grade=input("What is your letter grade(please use capitlization): ")
possible_grades = {"A","B+","B","C+","C","D","F"}
while letter_grade not in possible_grades:
    print ("Enter a valid letter")
    letter_grade=input("What is your letter grade(please use capitlization): ")

Note that using a set will allow faster lookup.

gommb
  • 1,121
  • 1
  • 7
  • 21
scharette
  • 9,437
  • 8
  • 33
  • 67