Непрерывный ввод - PullRequest
       5

Непрерывный ввод

0 голосов
/ 17 октября 2019

Как я могу сделать непрерывный ввод? Я хочу, чтобы игрок мог писать меню в любое время, когда он хочет, и не только при запуске программы?

print("\nWelcome to the nature center. What would you like to do?")
choice = ''
while choice != 'q':
    print("\n[1] Enter 1 to take a bicycle ride.")
    print("[2] Enter 2 to go for a run.")
    print("[3] Enter 3 to climb a mountain.")
    print("[q] Enter q to quit.")

choice = input("\nWhat would you like to do? ")

if choice == '1': 
    print("\nHere's a bicycle. Have fun!\n")
elif choice == '2':
    print("\nHere are some running shoes. Run fast!\n")
elif choice == '3':
    print("\nHere's a map. Can you leave a trip plan for us?\n")
elif choice == 'q':
    print("\nThanks for playing. See you later.\n")
else:
    print("\nI don't understand that choice, please try again.\n")

Ответы [ 2 ]

0 голосов
/ 17 октября 2019

Не уверен, что ваш отступ неправильный, но вы можете просто зациклить навсегда (while True), а затем просто break, когда вам нужно:

print("\nWelcome to the nature center. What would you like to do?")
while True: # Loop through indefinitely
    print("\n[1] Enter 1 to take a bicycle ride.")
    print("[2] Enter 2 to go for a run.")
    print("[3] Enter 3 to climb a mountain.")
    print("[q] Enter q to quit.")

    choice = input("\nWhat would you like to do? ")

    if choice == '1': 
        print("\nHere's a bicycle. Have fun!\n")
    elif choice == '2':
        print("\nHere are some running shoes. Run fast!\n")
    elif choice == '3':
        print("\nHere's a map. Can you leave a trip plan for us?\n")
    elif choice == 'q':
        print("\nThanks for playing. See you later.\n")
        break # Exit the loop here
    else:
        print("\nI don't understand that choice, please try again.\n")

Вывод:

Welcome to the nature center. What would you like to do?

[1] Enter 1 to take a bicycle ride.
[2] Enter 2 to go for a run.
[3] Enter 3 to climb a mountain.
[q] Enter q to quit.

What would you like to do? 1

Here's a bicycle. Have fun!


[1] Enter 1 to take a bicycle ride.
[2] Enter 2 to go for a run.
[3] Enter 3 to climb a mountain.
[q] Enter q to quit.

What would you like to do? 2

Here are some running shoes. Run fast!


[1] Enter 1 to take a bicycle ride.
[2] Enter 2 to go for a run.
[3] Enter 3 to climb a mountain.
[q] Enter q to quit.

What would you like to do? q

Thanks for playing. See you later.
0 голосов
/ 17 октября 2019

Я думаю, вам нужно также сделать "корпус переключателя" в вашем while:

print("\nWelcome to the nature center. What would you like to do?")
choice = ''
while choice != 'q':
    print("\n[1] Enter 1 to take a bicycle ride.")
    print("[2] Enter 2 to go for a run.")
    print("[3] Enter 3 to climb a mountain.")1
    print("[q] Enter q to quit.")

    choice = input("\nWhat would you like to do? ")

    if choice == '1': 
        print("\nHere's a bicycle. Have fun!\n")
    elif choice == '2':
        print("\nHere are some running shoes. Run fast!\n")
    elif choice == '3':
        print("\nHere's a map. Can you leave a trip plan for us?\n")
    elif choice == 'q':
        print("\nThanks for playing. See you later.\n")
    else:
        print("\nI don't understand that choice, please try again.\n")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...