Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable Not Updating
#1
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
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable not updating Rayaan 3 6,034 Mar-29-2020, 04:42 PM
Last Post: SheeppOSU
  Global Variable Not Updating joew 2 7,841 Jan-26-2020, 04:15 PM
Last Post: joew

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020