How to permanently change a variable in Python?

Question:

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 doesen’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 beggining with coding, and I am doing this so I can test my knowledge.
I left the code below.

def atm_machine():
    code  = input("Enter yout 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 coden""")
        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 yout 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 inccorect!")
        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()

Asked By: Vasi Stirbu

||

Answers:

I’m not sure if it’s a best way to do it, but I would create a txt file with atm pin which You could read in the beggining to take pin code and write it when You want to change it. Something like this:

def atm_machine():
    code = input("Enter yout PIN code:")
    pin_code = int(code)
    real_pin = open("code.txt", "r")     
    real_pin_code = int(real_pin.read())

Here I already have a txt file with some code created in program folder so python could read it, and here I’m saving new pin to same txt file:

else:
    real_pin = open("code.txt", "w")
    real_pin.write(str(changed_pin_code))
    print("PIN code succesfully changed!")
    print("Your new PIN code is:", changed_pin_code)
    atm_recall()

Hope this helps anyhow.

Answered By: Jahgodka

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 yout 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 coden""")
          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("Insufficent fund")

              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 yout 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 succesfully 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 inccorect!")
          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.

Answered By: Suraynsh-23
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.