简体   繁体   中英

Python text based game room to room movement

Hi I am new to coding and creating a text based game in python. I am writing the code to move from room to room. I need some help cleaning up what I have done so far. I ran it through an instant feedback program and got these tips and not exactly sure how to correct it:

  • The Great Hall string is a key in main dictionary. Revise code so it is not a key in main dictionary.
  • It said it can not classify my code (not sure what that means)
  • consolidate multiple print commands into one function
  • make condition simpler by using non-complex conditions
  • Better practice to use while True: and the break reserved word to stop the loop when you require it to stop

data setup

rooms = {'Great Hall': {'name': 'Great Hall', 'South': 'Bedroom', 'North': 'Dungeon', 'West': 
'Library', 'East': 'Kitchen'},
     
'Bedroom': {'name': 'Bedroom', 'East': 'Cellar', 'North': 'Great Hall'},
      
'Cellar': {'name': 'Cellar', 'West': 'Bedroom'},
     
'Library': {'name': 'Library', 'East': 'Great Hall'},
    
'Kitchen': {'name': 'Kitchen', 'West': 'Great Hall', 'North': 'Dining Room'},
     
'Dining Room': {'name': 'Dining Room', 'South': 'Kitchen'},
     
'Dungeon': {'name': 'Dungeon', 'South': 'Great Hall', 'East': 'Gallery'},
    
'Gallery': {'name': 'Gallery', 'West': 'Dungeon'},
    
}

directions = ['North', 'South', 'East', 'West']

current_room = rooms['Great Hall']

game loop

while True:

# display current location

print()

print('You are in the {}.'.format(current_room['name']))

# get user input
command = input('\nWhat direction do you want to go? ').strip()
# movement
if command in directions:
    if command in current_room:
        current_room = rooms[current_room[command]]
    else:
        # bad movement
        print("You can't go that way.")
# Exit game
elif command.lower() in ('q', 'quit'):
    break
# bad command
else:
    print("I don't understand that command.")

By the way, you will get more help on Code Review Stack Exchange. https://codereview.stackexchange.com/

First of all, your code does not work - the code in the While loop is not indented.

This will throw an error:

while True:
print("hi")

This will not:

while True:
    print("hi")
  • Better practice to use while True: and the break reserved word to stop the loop when you require it to stop

You seem to already be using that, but it's not noticing it because of your indentation error.

  • The Great Hall string is a key in main dictionary. Revise code so it is not a key in main dictionary.

What this might mean is making multiple dictionaries instead of putting it all in one, but that doesn't really matter so I don't know why it's complaining.

  • It said it can not classify my code (not sure what that means) I don't either; please provide some context.

  • consolidate multiple print commands into one function

Now this is getting out of hand. I don't know why it's telling you to do that, but you could use a tuple for that:

def printstuff(x):
    for i in x:
        print(i)

printstuff((5,5,3,85,"hi"))
# will print:
# 5
# 5
# 3
# 85
# hi 
  • make condition simpler by using non-complex conditions

Not sure what it means there. Again, please provide context.

Also, as a comment said, using f-strings (python 3.6+ only) would make your life a whole lot easier. This:

hello = "hello world"
print("\n{}".format(hello))

could be shortened to this:

hello = "hello world"
print(f"\n{hello}")

(the \n will act as a blank print() call)

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