1

This is most likely a very basic question as I just started Python the other day, but I couldn't find any documentation. I'm practising with a basic text-adventure program.

Here's what I have set up: The program goes though a course of specific questions. When the user gives input, that input is put through a function to see if it contains the keyword inv (inventory). If so, the program will display the inventory dictionary. If not, the function will return the original input value for the program to continue.

Here is my current "info" function

def info(s):
inputinventory = s
if inputinventory == 'inv':
    print ('\n###INVENTORY###')  

        #here it would print my dictionary - don't worry about this

    #asks user to input again to continue question if 'inv' has been typed
    info(input())
else:
    #script will return the original input value if 'inv' is not typed
    return inputinventory

Implementation of this function:

print ('How old are you? ')
if info(input()) == '12':   #just an example
    print ('some example text')
elif info(input()) == '13':  #the troublesome line
    print ('more example text')

The problem I am having is that the elif statement doesn't work as it should since it requires input. This is the way I researched to test the value of a return - I can't find out how else to get it without writing the function over and over again to get what it returns. Is there a way to solve this, or am I making things difficult?

edit: Wait - I bet I can just run the function once and assign it to a variable... Let's try that really quick... but that won't work if the user types "inv" probably

3 Answers 3

3

You want to store the return value of info() as a variable.

print ('How old are you? ')
user_value = info(input()) # store it is a variable
if user_value == '12':
    print ('some example text')
elif user_value == '13':
    print ('more example text') # it works!
Sign up to request clarification or add additional context in comments.

1 Comment

This does work fine, except if I first type in "inv" to display the inventory, it no longer displays text. I'm going to revise my code so I check if 'inv' is entered in a different spot. But winning answer still, thanks.
0

I think you'd want to call input(), and assign the result to a variable, BEFORE any of your ifs; then you test that variable as many ways as you like.

Comments

0

I would actually get a little more sophisticated

some_dictionary = {12: 'some example text', 13: 'some other example text', . . . }

print ('How old are you')

user_value = info(input()):

while user_value not in some_dictionary:
    print ('some warning message')
    print (' if you want to exit then type N')
    print ('How old are you')
    user_value = info(input())
    if user_value.upper() == 'N':
         break

if user_value in some_dictionary:
    print some_dictionary[user_value]

This way you get closer to making the code simpler and you can more easily modify and extend the code

2 Comments

This results in an infinite loop if the user does not enter a value in the dictionary. It would keep printing some warning message infinitely.
Thanks I was thinking about the big problem not the details - the story of my life

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.