[Solved] Changing string into integers for variables inside function, Python


The variable n inside the function is distinct from the expression (or variable) supplied as an argument. As such, assigning to n inside the function will not affect any of the variables outside. (This is because Python is not Call By Reference.)

Instead, use the return value, eg.

def classifyInput(n):    
    if n == "r":
        return 1
    elif n == "s":
        return 2
    elif n == "p":
        return 3
    else:
        print "Wrong input!"
        # implicit: return None

p1_inp = raw_input("Player 1 ?")
p1 = classifyInput(p1_inp)

4

solved Changing string into integers for variables inside function, Python