简体   繁体   中英

How to allow a user to quit a program

I am making a text adventure game and I am trying to allow the user to quit the game using q. I don't know what to input, here's my code.

print ("Welcome to Camel!")
print ("You have stolen a camel to make your way across the great Mobi deset.")
print ("The natives want their camel back and are chasing you down! Survive your desert trek and out run the natives.")

print ("Welcome to Camel!")
print ("You have stolen a camel to make your way across the great Mobi deset.")
print ("The natives want their camel back and are chasing you down! Survive your desert trek and out run the natives.")

done = False
while done == False:
    print("Print the letter only.")
    print("A. Drink from your canteen.")
    print("B. Ahead moderate speed.")
    print("C. Ahead full speed.")
    print("D. Stop for the night.")
    print("E. Status check.")
    print("Q. Quit.")

    first_question = input("What do you want to do? ")

    if first_question.lower() == "q":
        done = True

    if done == True:
        quit

The lines are all indented properly in the code (the website makes it weird when copy pasting). Any help is appreciated.

Your program is nearly fine as is, and in fact it runs just fine in python3. The problem is that in python2, input will actually evaluate your input. In order to support python2, use raw_input .

print ("Welcome to Camel!")
print ("You have stolen a camel to make your way across the great Mobi deset.")

print ("The natives want their camel back and are chasing you down! Survive your desert trek and out run the natives.")

done = False

while done == False:

    print("Print the letter only.")

    print("A. Drink from your canteen.")

    print("B. Ahead moderate speed.")

    print("C. Ahead full speed.")

    print("D. Stop for the night.")

    print("E. Status check.")

    print("Q. Quit.")

    first_question = input("What do you want to do? ")

    if first_question.lower() == "q":

        done = True

        if done == True:

            break

Now, when you input 'Q' or 'q' program will quit. It's about tabulation. Is that what you asked?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM