Python Forum
Variable Not Updating - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Variable Not Updating (/thread-27468.html)



Variable Not Updating - NectDz - Jun-07-2020

Hello, Im having a problem with my function. It is supposed to out put '01' if the user only puts 1 digit but when I call it in the engine function it does not update.

def engine():
    m = int(input("Type the Mintue: "))
    m = str(m)
    check(m)
    print(m)

def check(m):
    if len(m) != 2:
        for num in m:
            m = "0" + str(m)
    return



RE: Variable Not Updating - bowlofred - Jun-07-2020

Strings in python are immutable. If you try to change them, you end up making a changed copy, not changing the original.

Therefore the check() function above does nothing. When you attempt to change it on line 10, it changes a copy. The m in engine() isn't touched.

If you want to change a string in a function, you probably want to return the changes and have the caller accept them.

def check(m):
    if len(m) != 2:
        for num in m:
            m = "0" + str(m)
    return m

m = check(m)