Search code examples
pythonvariablesassign

How to permanently change a variable in Python?


I decided to make a code in Python that is like an ATM Machine, and Everything works very well, you can check your balance, extract or add money,and change the PIN code. But when I change the PIN code I tried to assign the "changed_pin_code" variable to the "real_pin_code" variable,and it doesn't work, the PIN code remains the same. If you could give me some ideas on how I could solve the problem, or improve my code, that would be fantastic.

I am at the beginning with coding, and I am doing this so I can test my knowledge.

Here's my code:

def atm_machine():
    code  = input("Enter your PIN code:")
    pin_code = int(code)
    real_pin_code = 4137
    balance = 10000
    if pin_code == real_pin_code:
        print("""
          ATM   
    1)Check Balance
    2)Add money
    3)Extract money
    4)Change PIN code\n""")
        number_select = int(input("Select a number:"))
        
        if number_select > 4 and number_select == 0:
            print("You must select a number between 1 and 4!")
            atm_recall()

        if number_select == 1:
            print("Your current balance is:", balance, "$")

            atm_recall()

        if number_select == 2:
            money_add = int(input("Enter amount of money you want to add:"))
            new_money = balance + money_add
            print("Your current balance is:", new_money, "$")
            atm_recall()

        if number_select == 3:
            money_extract = int(input("Enter the amount of money you want to extract:"))

            if money_extract > balance:
                print("Insufficent fund")
                atm_recall()

            if money_extract <= balance:
                remained_money = balance - money_extract
                balance = remained_money
                print("Your current balance is:", remained_money, "$")
                atm_recall()
    
        if number_select == 4: 
            measure_pin = 9999
            changed_pin_code = int(input("Enter new PIN code:"))

            if changed_pin_code == real_pin_code:
                print("You can't enter the same PIN code:")
                print("Wait for your card!")
                atm_recall()

            if changed_pin_code > measure_pin:
                print("PIN code must be formed of 4 digits!")
                print("Wait for your card")
                atm_recall()

            else:
                real_pin_code = changed_pin_code
                print("PIN code succesfully changed!")
                print("Your new PIN code is:", changed_pin_code)
                atm_recall()
    
    else:
        print("PIN code incorrect!")
        print("Wait for your card!")

def atm_recall():
    question = str(input("To make another action, type \"Y\",else, type\"N\" "))
    if question == "Y":
        result = atm_machine()
        return result
    if question == "N":
        print("Good Bye!")
        print("Wait for your card!")
atm_machine()


Solution

  • The problem you are currently facing is because at end of each option you call atm_recall function, in which if the user selects the 'Y' option, it calls the atm_machine function which whenever called starts with real_pin_code set to 4137.

    Solution

    As already mentioned by others, in comments what you should rather do is get rid of the recursion as it uses up a lot of memory and fills up the call stack on each function call. So a refactored approach would be something like this:

    real_pin_code = 4137
    balance = 10000
    while True:
          code = input("Enter your PIN code:")
          pin_code = int(code)
          if pin_code == real_pin_code:
              print("""
                ATM   
          1)Check Balance
          2)Add money
          3)Extract money
          4)Change PIN code\n""")
              number_select = int(input("Select a number:"))
              
              if number_select == 1:
                  print("Your current balance is:", balance, "$")
    
    
              elif number_select == 2:
                  money_add = int(input("Enter amount of money you want to add:"))
                  balance = balance + money_add
                  print("Your current balance is:", balance, "$")
    
              elif number_select == 3:
                  money_extract = int(input("Enter the amount of money you want to extract:"))
    
                  if money_extract > balance:
                      print("Insufficient funds")
    
                  else:
                      remained_money = balance - money_extract
                      balance = remained_money
                      print("Your current balance is:", remained_money, "$")
          
              elif number_select == 4: 
                  measure_pin = 9999
                  changed_pin_code = int(input("Enter new PIN code:"))
    
                  if changed_pin_code == real_pin_code:
                      print("You can't enter the same PIN code:")
                      print("Wait for your card!")
    
                  elif changed_pin_code > measure_pin:
                      print("PIN code must be formed of 4 digits!")
                      print("Wait for your card")
    
                  else:
                      real_pin_code = changed_pin_code
                      print("PIN code successfully changed!")
                      print("Your new PIN code is:", changed_pin_code)
              else:
                  print("You must select a number between 1 and 4!")
          else:
              print("PIN code incorrect!")
              print("Wait for your card!")
    
          question = str(input("To make another action, type \"Y\",else, type\"N\" "))
          if question == "N":
              print("Good Bye!")
              print("Wait for your card!")
              break
    

    Changes

    • I took real_pin_code out of the loop, so its value doesn't reset at each iteration
    • Also, for the same reason I took balance out of the loop and made the changes to balance itself rather than declaring a new variable new_money
    • Also, as a good programming practice I changed a lot of if statements to elif and else where the conditions were exclusive of each other

    Better Approach

    A better approach, as already highlighted by others would be to use class where you would initialize an ATMMachine object with all the required details. However the approach you choose, mostly all the logic would remain the same except some refactoring and specific methods for each task.