This is my first post. I'm working on the famous "Dragon Text Game" project for new programmers of Python. I've gotten most of my base code working, except for some reason, I can't figure out why my code is testing negative for being able to move into a room. I'll include snippets of my code below. They appear in this order.
I have this (piece of) dictionary for the rooms (it's a Norse-themed game! :3 )...
realms = {
'Midgard': {'North': 'Asgard', 'South': 'Muspelheim', 'East': 'Vanaheim', 'West': 'Alfheim'},
'Asgard': {'North': 'Jotunheim', 'South': 'Midgard', 'East': 'Niflheim'}
}
This is a couple of functions I've used to see whether or not the player can move north into a room.
DIRECTION_NORTH = 'North'
def can_go_north(current_world):
return current_world in realms and DIRECTION_NORTH in realms[current_world]
def go_north(current_world):
if can_go_north(current_world):
updated_world = realms[current_world][DIRECTION_NORTH]
print(f'You are now in the realm of {updated_world}.')
print(realm_intro[updated_world])
else:
updated_world = current_world
print("You cannot go North from here. Try again.")
return updated_world
Lastly, this is in the main body of code where I'm implementing the functions in the game.
while current_realm != 'Hel':
print("Enter a command. Enter \"Help\" for list of commands.")
command = input()
if command == "Help":
print('Commands: Get Item, Go North, Go South, Go East, Go West, Help, Inventory.')
elif command == "Inventory" :
print(inventory)
print('You have ',len(inventory),' of seven relics.')
elif command == "Get Item":
get_item(current_realm)
elif command == "Go North" :
current_realm = go_north(current_realm)
else :
print("Invalid command. Enter 'Help' for command list.")
if current_realm == 'Hel':
endgame()
Every time I run the game and attempt to move North from Midgard (starting room) into Asgard, my program blocks me and prints "You cannot go North from here. Try again." I wonder if I mistyped anything in my dictionary syntax-wise, but I've checked over and over again. I've also had help with the AI in PyCharm, but it can't seem to catch my problem.
Anyone got any ideas? Thanks so much! :D
Your code is missing a lot of critical information, so I'll do my best to explain and answer as I go along.
I made a quick reconstruction of the code provided and added some additional items, see below.
def main():
current_realm = 'Midgard'
def can_go_north(current_world):
return current_world in realms and DIRECTION_NORTH in realms[current_world]
(This may or may not be similar to your code, but considering the lack of information, hopefully it suffices and can allow you to see some similarity and help you visualize how to fix your issue.)
In your code snippets, we don't see when/how you're actually initializing your "current_realm" variable, which can be very important based on when the usage is occurring. In the full reconstruction above, it seems to work (at least to the efficacy of what has been provided and asked of), so it looks like you may simply need to initialize your variable earlier than you are or make sure that your casing is proper.
To avoid casing issues, it might be helpful to automatically change the user's input to lowercase or to uppercase, depending on how you want to deal with it in your code.
Example:
command = input().lower()
OR
command = input().upper()
Furthermore,
If you are a new programmer, please refrain from using AI to do any completion of your work or to help you in any major way. AI will try to provide a correction, and most AI-based corrections are based off of huge repositories that the LLM corporations collect from all over the web; Many of these sources are not reliable and generally contain poor coding practices.
For future questions, it might be helpful to provide a section of which area of your code you THINK the problem is and then provide all of your code in a PasteBin link. Providing the area of code you think the problem is in AND providing specific examples of things that you have tried to do will show the users commenting and answering here what you have tried and that you have tried in the first place.
Hopefully the above has helped you in some way, if not, please try providing a bit more context.
Happy coding!