简体   繁体   中英

How do I get my code to loop back to an input once it has been executed already? (RPG Game)

I'm a beginner at Python and thought it would be fun to code a text-based RPG game. It is by no means efficient, I'm just doing it for the fun of it.

I want my code to loop back to a specific decision path I have created where you can choose to do different actions. These actions are different subroutines that it calls depending on what you enter. However, I can't figure out how to get it to loop back once you have already chosen and completed an action.

Here is the code:

print("\n~DECISION PATH~\n\n[READ NOTICE BOARD][TALK TO WIZARD][TALK TO BLACKSMITH][TALK TO ARMOURER][VIEW STATS]")
action=input("\nWhat will you do? ")

if action == "Read notice board":
    noticeBoard()
elif action == "Talk to wizard":
    wizard()
elif action == "Talk to blacksmith":
    blacksmith()
elif action == "Talk to armourer":
    armourer()
elif action == "View stats":
    viewStats()

If somebody could help me out that would be great. Thanks!

Add a while loop with condition = True if you want the user to play it again and again. You can add a condition such that exit the game if the user presses a key if you prefer that.

print("\n~DECISION PATH~\n\n[READ NOTICE BOARD][TALK TO WIZARD][TALK TO BLACKSMITH][TALK TO ARMOURER][VIEW STATS]")

while(True):
    action=input("\nWhat will you do? ")

    if action == "Read notice board":
        noticeBoard()
    elif action == "Talk to wizard":
        wizard()
    elif action == "Talk to blacksmith":
        blacksmith()
    elif action == "Talk to armourer":
        armourer()
    elif action == "View stats":
        viewStats()

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