vendredi 26 août 2016

Python: Function not holding the value of a variable after execution

I'm trying to convert a string input (y/n) into an integer (1/0). The function appears to be working, as when the 'convert' function is executed using 'a' as an argument, the argument is printed inside the function, however printing outside the function uses the original value of the variable 'a'. I've tried using return inside the 'convert' function, but this appears to have no affect.

a = input("Are you happy?(y/n)")

def convert(x):
    if x == ('y'):
        x = 1
    if x == ('n'):
        x = 0
    print (x)

convert(a)
print (a)

>>> Are you happy?(y/n)y
1
y

Aucun commentaire:

Enregistrer un commentaire