1

I am struggling with local and global variables.

I was doing this:

def SomeFunc(Input):
    if Input == 1:
        Input = "True"
    elif Input == 0:
        Input = "False"

RawInput=int(raw_input("Input 1 or 0: "))
SomeFunc(RawInput)
print str(RawInput)

and it showed this:

>>> def SomeFunc(Input):
    if Input ==1:
        Input = "True"
    elif Input ==0:
        Input = "False"

>>> RawInput = int(raw_input("Input 1 or 0: "))
Input 1 or 0: 1
>>> SomeFunc(RawInput)
>>> print str(RawInput)
1

What should I do so that the input will convert to str in the function?

9
  • 3
    Use lowercase_with_underscores for variable names/functions python.org/dev/peps/pep-0008 Commented Jul 13, 2012 at 2:26
  • 1
    The best way to do this is to not have any side effects and return the value from the function btw Commented Jul 13, 2012 at 2:28
  • if you wanted, you could reduce this to Input = str(bool(raw_input("Input 1 or 0: "))). The string '0' converts to the bool False which converts to the string "False" Commented Jul 13, 2012 at 2:55
  • @xhainingx -- Nope. bool('0') is True since '0' is a non-empty string. Commented Jul 13, 2012 at 3:02
  • @mgilson woops, sorry, shouldve been Input = str(bool(int(raw_input("Input 1 or 0: ")))) Commented Jul 13, 2012 at 3:08

4 Answers 4

4

Input is an int which is an immutable object. In other words, you can't change it anywhere. However, you can assign it to something else. One of the tricky things about python is learning that everything is just a reference. When you assign to something, you just change the reference. Think about your variables as strings which go into boxes. When you assign to a variable, you put that string in a particular box. When you assign that variable to something else, you change the box that string goes to. Of course, when you actually do an addition, you add the contents of the box, not the strings (which is a little confusing). If you're accustomed to programming in C, it's kind of like everything is a pointer which gets automatically dereferenced (except when you do an assignment).

So, what to do? The easiest thing is to return the value from your function.

def some_func(inpt):
    if inpt == 1:
        return "True"
    elif inpt == 0:
        return "False"
    else:
        raise ValueError("WHAT IS THIS GARBAGE? I SAID 0 OR 1!!!!") # ;^)

Now you can call your function as:

processed_input = some_func(rw_inpt)

as a side note, your function can be condensed to:

def some_func(inpt):
    return str(bool(inpt))

or

def some_func(inpt):
    return "True" if inpt else "False"

Both of these pass (and return "True" if the user puts in any integer that isn't 0). If you really want to force the user to not put in something like "2", you can do:

def some_func(inpt):
    return {1:"True",0:"False"}[inpt]

But I wouldn't recommend that one...

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, one more question for you. I am doing a hex to dec converter, i want to do a checking for the value if 10 then A, 11 then b etc. and i tried many ways but its working. most important i cant applied the above in the converter.
@Zaia -- python has that builtin. See hex and int. Specifically, int(hex(10),16) which converts 10 to hex (0xa) and then back to 10 (decimal).
1

Basically you want the argument to be pass-by-reference, but that is not how Python works for basic datatypes. The pythonic way of doing this is as following:

def some_func(input):
    if input == 1:
        return "True"
    elif input == 0:
        return "False"

And then

raw_input = some_func(raw_input)

Comments

1

Note: You can just do this: print bool(int(raw_input("Input 1 or 0: ")))

The more pythonic way of doing it would be as follows:

def some_func(x):
    if x == 1:
        return "True"
    elif x == 0:
        return "False"

x = int(raw_input("Input 1 or 0: "))
x = some_func(x)
print x

However if you did want to use globals you could do it like this but this is really hacky and it isn't one of the good coding practices that python promotes.

def some_func():
    global x
    if x == 1:
        x = "True"
    elif x == 0:
        x = "False"

x = int(raw_input("Input 1 or 0: "))
some_func()
print x

Some other notes: Try not to use names like the bultins eg. Input and RawInput since it makes things unclear and mixes everything up.

Comments

0

What should I do so that the input will convert to str in the function?

def SomeFunc(Input):
    as_string = str(Input)
    if Input == '1':
        Input = "True"
    elif Input == '0':
        Input = "False"

Note that python has a native boolean type, including the values True and False already. You can use the integer value of Input as a condition all on its own.

1 Comment

Haters, please add constructive criticism about down voted answers.

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.