Is there a better way to make multiple choice look commands in a text based game?

Viewed 41

I am making a text based adventure game with commands to look at the environment aswell as details within the environment; I am curious if there is a better way to make multiple choice look commands, maybe some way to lump all the elif statements together as they all do the same thing anyways.

if choice == "look river":
        print(f"{Back.BLUE}The water is merky.{Style.RESET_ALL}")
        print(f"{Fore.YELLOW}There is something in the river reflecting sunlight.{Style.RESET_ALL}")
elif choice =="look river bank":
        print(f"{Back.BLUE}The water is merky.{Style.RESET_ALL}")
        print(f"{Fore.YELLOW}There is something in the river reflecting sunlight.{Style.RESET_ALL}")
elif choice =="look water":
        print(f"{Back.BLUE}The water is merky.{Style.RESET_ALL}")
        print(f"{Fore.YELLOW}There is something in the river 
1 Answers

You could create a dictionary with all the different possible 'look' commands:

choices_look = {'look river':"the water is merky\nYou can see your reflection",
"look riverbank":"you have looked at the river bank"}

and then use a function to find the correct response to a command:

def get_choice():
    choice = str(input("What do you want to do? ").lower())
    if 'look'in choice:
        for i in choices_look:
            if i == choice:
                print(choices_look[i])

This is what I have done in one of my text based games and I found it made the code a lot neater than having a lot of if and elif statements.

If you wanted the possibility of having more than one response to a command and still use the dictionary then you can make the value a list:

choice_looks = {'look river':['you looked at the river','something else']}

You can then choose to only show one of the responses based on a condition or show both whilst the code still being clumped together in a somewhat neat manner.

Hope that helps.

Related