I am building a text based game for a class. This isn't the game I'm kind of testing the foundation separately. I'm aware of the misspellings and messiness but i'll still take advice on that as i tend to struggle with "style" a lot.
I feel like i'm right on the verge of solving this... There's a dictionary of areas and items and I want the play to have to type "Go" first for a direction or "Get" first if they want to retrieve an Item. This took me so long to figure out and yet once I finally got I i ran into other errors and checks and balances I can't quite seem to wrap my head around.
Right now what works is as follows
- player typing anything other than go/get, an error message occurs
- player typing go/get then a direction or item that doesnt exit, an error message occours
- If the player tries to receive an Item that DOESN'T exist in a room that DOES contain an Item an error message occurs
- if the player types exit the program ends
this is almost perfect.
while action != 'exit':
status()
print(inventory)
action = input('> ').strip().title()
splitaction = action.split()
if splitaction[0] == 'Go'.strip().title():
if splitaction[1] in rooms[current_room]:
current_room = rooms[current_room][splitaction[1]]
print(f'You went {splitaction[1]}')
elif splitaction[1] not in rooms[current_room]:
print('u cant do tht')
if splitaction[0] == 'Get'.strip().title():
item = rooms[current_room].get('Item')
if splitaction[1] not in rooms[current_room].get('Item'):
print('tht item no exists here')
elif splitaction[1] in rooms[current_room].get('Item'):
if item not in inventory:
inventory.append(rooms[current_room].get('Item'))
print(f'You got the {item}!')
elif item in inventory:
print('u alrdy got tht')
elif action == 'exit'.strip().title():
print("You've chosen to end the game, goodbye.")
snooze(2)
break
Right now my current problems
- If the player simply types Go/Get and nothing else I get the following error:
if splitaction[1] in rooms[current_room]:
~~~~~~~~~~~^^^
IndexError: list index out of range
- If the player trys to receive an item in a room that DOESN'T contain one I get the following error:
if splitaction[1] not in rooms[current_room].get('Item'):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: argument of type 'NoneType' is not iterable
I can't seem to wrap my head around how to fix and print error messages for these occassions? I wouldn't be surprise if something very simple is going over my head, my brain got fried from simply making the Go/Get 'kind of' work.
I appreciate you all in advance!
Thing's I've tried:
if splitaction[1] not in rooms[current_room].get('Item'):
expected to get error message since there's no item in that room but instead get NoneType not iterable. I properly get the error message only if the room has an item but the one typed doesn't exist.
also tried
if len(splitaction) <= 1
then print an error message so it could cover the player simply typing go/get but I still get the out of range error
tried
if splitaction[1] == none
for the same effect but nothing is working
i tried to use a Try/Except but I got the same errors I don't quite know how it works yet. I'm a complete beginner and we didn't go over that in class yet
Update: Thanks to 0x01010 I was able to fix the out of range issue but i'm still struggling with the NoneType Error:
if splitaction[0] == 'Get'.strip().title():
item = rooms[current_room].get('Item')
if getelement(splitaction, 1) not in rooms[current_room]:
print("That item doesn't exist")
elif item in rooms[current_room].get("Item"):
if item not in inventory:
inventory.append(rooms[current_room].get('Item'))
print(f'You got the {item}!')
elif item in inventory:
print('You already have that item!')
Right now I get the "Item doesn't exist" even if the item does exist in the current room? If I change the the first nested if statement to rooms[current_room].get('item') it brings me back to the nonetype error if the player tries to receive an item in a room that doesn't have one. It works fine if the room does have an item. I'm almost at the final point where the game is at least playable even if not perfect but I don't want the program to crash simply because the player tries to receive an item in a room that doesn't have one.
source https://stackoverflow.com/questions/76021133/how-to-output-error-messages-if-split-userinput-is-out-of-range
Comments
Post a Comment