Python Game is not ending

Question:

ans = True
while ans:
    print("""
    1.Add a Student
    2.Delete a Student
    3.Look Up Student Record
    4.Exit/Quit
    """)
    ans = input("What would you like to do? ")
    if ans == "1":
        print("n Student Added")
    elif ans == "2":
        print("n Student Deleted")
    elif ans == "3":
        print("n Student Record Found")
    elif ans == "4":
        print("n Goodbye")
    elif ans != "":
        print("n Not Valid Choice Try again")
Asked By: Sphnix Ken

||

Answers:

Your while loop condition is testing the True/False-ness of an input string, which is probably always True.

Answered By: craigwashere

It looks like the code is stuck in an infinite loop because the ans variable is always True, and there is no way to change its value to break out of the loop. One way to fix this is to initialize ans to a non-True value, and then change its value based on user input:

ans = None
while ans != "4":
    print("""
    1.Add a Student
    2.Delete a Student
    3.Look Up Student Record
    4.Exit/Quit
    """)
    ans = input("What would you like to do? ")
    if ans == "1":
        print("n Student Added")
    elif ans == "2":
        print("n Student Deleted")
    elif ans == "3":
        print("n Student Record Found")
    elif ans == "4":
        print("n Goodbye")
    else:
        print("n Not Valid Choice Try again")
Answered By: Anthony
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.