亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

連續輸入。

連續輸入。

一只甜甜圈 2022-06-28 10:25:46
如何進行連續輸入?我希望玩家能夠在他想要的任何時候編寫菜單,而不僅僅是在程序開始時?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 回答

?
郎朗坤

TA貢獻1921條經驗 獲得超9個贊

不確定您的縮進是否不正確,但您可以永遠循環(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.


查看完整回答
反對 回復 2022-06-28
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

我認為您還需要在您的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")


查看完整回答
反對 回復 2022-06-28
  • 2 回答
  • 0 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號