Skip to main content

How to output error messages if split userinput is out of range?

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

Popular posts from this blog

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to split a rinex file if I need 24 hours data

Trying to divide rinex file using the command gfzrnx but getting this error. While doing that getting this error msg 'gfzrnx' is not recognized as an internal or external command Trying to split rinex file using the command gfzrnx. also install'gfzrnx'. my doubt is I need to run this program in 'gfzrnx' or in 'cmdprompt'. I am expecting a rinex file with 24 hrs or 1 day data.I Have 48 hrs data in RINEX format. Please help me to solve this issue. source https://stackoverflow.com/questions/75385367/how-to-split-a-rinex-file-if-i-need-24-hours-data