简体   繁体   中英

How to accept an answer from two if statements in python

I have a question regarding nested if statements in python code. I was wondering if I could have two if statements in a while loop that both have a nested if statement inside. Then the person would be able to either reply from a nested if statement or the other while loop if statement. Any thoughts on my code would be appreciated.

This is a code sample of a game I am trying to make. I am trying to learn and incorporate new skills into it and so here is my question.

Is there a way to make it that when I type "look forward" into the question input and for the second input I say "look backward" instead of "go there" then the input could be for the question input instead of the move_on input?

def first_room():
    print("Your in a room how to get out...")
    next_room = True
    while next_room:
        question = input()
        if question == "look forward".lower():
            print("You are looking forward")
            move_on = input()
            if move_on == 'go there'.lower():
                next_room = False
                second_room()
            else:
                pass
        if question == 'look backward'.lower():
            print("You are looking backward")
            move_on = input()
            if move_on == 'go there'.lower():
                next_room = False
                third_room()
            else:
                pass


def second_room():
    print("This is the second room")


def third_room():
    print("This is the third room") 


first_room()

I can' test it but I think it could be something like this

I use variables with True/False to remeber "state" of game

BTW: If your run one room inside another function you will have hidden "recursion". It would be better to return function name (without () ) and run it outside function.

def first_room():
    print("Your in a room how to get out...")

    looking_forward = False
    looking_backward = False

    while True:
        question = input().lower()
        if question == "look forward":
            if not looking_forward:
                print("You are looking forward")
                looking_forward = True
                looking_backward = False
            else:            
                print("You are already looking forward, so what next")
        elif question == 'look backward':
            if not looking_backward:
                print("You are looking backward")
                looking_forward = False
                looking_backward = True
            else:            
                print("You are already looking backward, so what next")
        elif question == 'go there':
            if looking_forward:
                return second_room # return function name to execute outside
            if looking_backward:
                return third_room # return function name to execute outside
            else:
                print('Go where ???')


def second_room():
    print("This is the second room")


def third_room():
    print("This is the third room") 

# --------------------------------

next_room = first_room # assign function without ()

while next_room is not None:
    next_room = next_room() # get function returned from function 

print("Good Bye")    

Inspired by furas' remark about recursion, I was wondering how you could handle any room with a general function. In the current design, many rooms require many function, which will use a lot of the same code over and over again. If every room is 'special' in some way that requires specific code, this may be unavoidable. On the other hand you can save yourself a lot of work if you have a generic way to navigate the rooms.

So I was thinking, perhaps you could store room attributes in some common form and have a general function that asks for player input and suggests what they can do. As the 'common form' I chose a list of dictionaries here, figuring it would be the easiest to work with.

Each list index (+1 for zero-indexing) corresponds to a room number, eg roomlist[0] is the first room. And each dictionary stores what we need to know about the room, eg which directions you can look and which room that direction connects to. You can also expand the dictionaries with additional information about each room later on. (Such as special features in some rooms)

My attempt at this:

# Room dictionaries: Each key is a direction and returns the
# room number it connects to. E.g. room1 'forward' goes to room2.
room1 = {'forward': 2, 
         'backward': 3}

room2 = {'forward': 4, 
         'backward': 1,
         'left': 3}

room3 = {'forward': 5, 
         'backward': 1,
         'right': 2}

roomlist = [room1, room2, room3]

def explore_room(roomindex, room):

    # roomindex is the list index (not room number) of the selected room.
    # room is the dictionary describing the selected room.

    question = None
    move_on = None

    print("You are in room %s, how to get out..." % (roomindex+1))
    # Room number added to help you keep track of where you are.

    next_room = True
    while next_room:

        if question == 'exit' or move_on == 'exit':
            break
            # If the user replied 'exit' to either question the loop breaks.
            # Just to avoid an infinite loop.

        question = input().lower()
        # Added the .lower here so we don't have to repeat it in each if.

        # Pick a direction. The response must be valid AND
        # it must be a valid direction for the current room.
        if question == "look forward" and 'forward' in room.keys():
            print("You are looking forward")
            direction = 'forward'
        elif question == 'look backward' and 'backward' in room.keys():
            print("You are looking backward")
            direction = 'backward'
        elif question == 'look left' and 'left' in room.keys():
            print('You are looking to the left')
            direction = 'left'
        # ...
        else:
            print('There is nowhere else to go!')
            continue 
            # Start the loop over again to make the user pick
            # a valid direction.

        # Choose to move on or stay put.
        move_on = input().lower()

        if move_on == 'yes':
            roomindex = room[direction]-1
            # Direction is the dictionary key for the selected direction.
            # Room number is roomindex-1 due to zero-indexing of lists.
            return roomindex
            # Exits explore_room and tells you which room to go to next.

        elif move_on == 'no':
            continue # Ask for a new direction.

        else:
            print('Please answer yes or no')
            continue # Ask for a new direction.

        return None 
        # No room selected. While return None is implicit I included it here for clarity.


roomindex = 0

while True: 
    if roomindex is None:
        # User exited the function without choosing a new room to go to.
        break

    roomindex = explore_room(roomindex, roomlist[roomindex])

You are in room 1, how to get out...

look forward

You are looking forward

yes

You are in room 2, how to get out...

look left

You are looking to the left

yes

You are in room 3, how to get out...

look backward

You are looking backward

yes

You are in room 1, how to get out...

exit There is nowhere else to go!

>

The yes/no thing is not the most inspired approach, I confess. You might want to add actual questions to the inputs anyway, just so the player can tell what is expected of them. But in general this seems to work and you could add and interconnect as many rooms as you liked fairly easily. All of them should still work with just one function.

Regarding the chaining and nesting of ifs: Nested ifs are generally okay but, as furas said, they may get out of hand and bloat your code. In your case you don't need nested ifs, however. As shown in my example, you can ask for direction first and then check if the player wants to go there separately . The two are completely independent, so there is no need for nested statements.

Edit

A basic single-question variant, again inspired by furas' original answer:

def explore_room(roomindex, room):

    # roomindex is the list index of the selected room.
    # room is the dictionary describing the selected room.

    question = None

    print("You are in room %s, how to get out..." % (roomindex+1))
    # Roomnumber added to help you keep track of where you are.

    next_room = True
    while next_room:

        if question == 'exit':
            break
            # If the user replied 'exit' to either question the loop breaks.
            # Just to avoid an infinite loop.

        question = input('Choose an action: ').lower()
        # Added the .lower here so we don't have to repeat it each time.

        # Pick a direction. The response must be valid AND
        # it must be a valid direction in this room.
        if question == "look forward" and 'forward' in room.keys():
            print("You are looking forward")
            direction = 'forward'
        elif question == 'look backward' and 'backward' in room.keys():
            print("You are looking backward")
            direction = 'backward'
        elif question == 'look left' and 'left' in room.keys():
            print('You are looking to the left')
            direction = 'left'
        # ...
        elif question == 'move': 
            roomindex = room[direction]-1
            return roomindex
            # Fails if player selects 'move' before picking a direction.
            # It's up to you how you want to handle that scenario.
        else:
            print('Valid actions are "look forward/backward/left/right" and "move". Enter "exit" to quit.')
            continue 
            # Start the loop over again to make the user pick
            # a valid direction.
    return None

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