1

I'm a casual gamer and hobbyist programmer who knows a bit of Python. I'm trying to make a simple text adventure game engine, so I need to get raw input from the player.

This is Python 3.2.2, so my line is this:

var = input("What do you want to do? ").lower()

And that line works, but rather than typing that whole line I'd like to make it into a function (something like "getinput()"). From what I've read about input() and functions I'm looking for a function that doesn't return anything, but changes another variable's state (here "var") as a "side effect."

I do have a working function "halt()" that takes no arguments:

def halt():
    input("(Press Enter to continue...) ")
    print("")

Calling "halt()" gives the prompt, waits for Enter, then prints a blank line and moves on, which I intended.

The function I'm trying to get to work looks like this:

def getinput(x):
    x = input("What do you want to do? ").lower()
    print("")

After defining getinput(x):

var = ""
getinput(var)
print(var)

That snippet does not print the user's input, and I'm confused as to why. What do I need to do to make this work in the intended fashion?

Is what I'm trying to do impossible with a function, or is there just something I don't know about scope? Should I be at codereview?

Community
  • 1
  • 1
Mike Embick
  • 77
  • 1
  • 7
  • 3
    I don't quite understand why you want to make your `getinput()` function not return anything. Your code will be easier to understand if your function returns the input, since you'll be working *with* Python instead of *against* it. – Greg Hewgill Apr 13 '12 at 02:10
  • This has been addressed many times, for one see http://stackoverflow.com/questions/986006/python-how-do-i-pass-a-variable-by-reference – Mark Ransom Apr 13 '12 at 02:32
  • If you want to get information from a function, then return information. That's what return values are for. – Karl Knechtel Apr 13 '12 at 04:13
  • Thanks, everyone for helping out. Sorry I wasn't able to respond; posting this question was the last thing I did before I went to sleep. @Mark Ransom I guess that wasn't one of the things that came up when I was looking for a previous answer. I pretty much searched for what terms are in the title of this question, but as it turns out I'm not supposed to be trying to work the code backwards. – Mike Embick Apr 13 '12 at 11:18

4 Answers4

4

You are right that the issue is about scope. The x in this line:

x = input("What do you want to do? ").lower()

Does not change the value that is passed to it- it creates a new variable (also called x) in the local scope of the function.

The right way to do this would be:

def getinput():
    return input("What do you want to do? ").lower()

x = getinput()
print(x)
David Robinson
  • 77,383
  • 16
  • 167
  • 187
2

NOTE: If you are going to use any version of Python before 3.x, definitely consider using the raw_input() function as the plain input() function only takes input that is SYNTACTICALLY VALID from the user, otherwise a SyntaxError will be raised.

I'm not sure what you're trying to do exactly, but I've moved around what you've written above so that it will work. Here's what I suggest trying:

First the function getinput()...

def getinput():
    x = raw_input("What do you want to do? ").lower() #prompts for the value of x
    print "" #prints a blank line
    return x

Then the second part...

var = getinput()
print(var)
pepperdreamteam
  • 2,772
  • 3
  • 17
  • 15
  • 1
    Your advice about `raw_input()` and `input()` is valid for Python 2.x, but in Python 3.x `raw_input()` was renamed to `input()`. That is, the OP was doing the right thing with `input()`. – Li-aung Yip Apr 13 '12 at 02:30
  • 1
    Don't worry, I did the *exact* same thing while answering a question a few weeks ago. Pay it forwards. :3 – Li-aung Yip Apr 13 '12 at 02:45
1

When you pass something to a Python function, it's generally impossible to modify it unless it's mutable. That restricts you to a small subset of Python types such as a list or dictionary.

def getinput(x):
    x[0] = input("What do you want to do? ").lower()
    print("")

var = [""]
getinput(var)
print(var[0])

You're far better off letting the function return a value. That's the way Python was meant to work.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

var is a python string variable which is being passed by value to your getinput function, which means that your getinput function only modifies a local copy of the name "x", not the value pointed to by "x" from your calling scope.

Also in Python strings are immutable and so it is impossible to modify a string's underlying value in-place due to string interning/hash consing - you merely create a new string. You should really be returning your value:

x = getinput()

But if you still want to stick to your existing design, you can pass this string variable by reference by wrapping it in a list or other reference type:

def getinput(li):
    li.append(input("What do you want to do? ").lower())
    print("")

usage:

x = []
getinput(x)
print x[0]
Preet Kukreti
  • 8,417
  • 28
  • 36