-1

I have to create an ATM style program wherein the code looks something like this:

import sys
def ATM():
    bank = 0
    coins = int (input ("Enter coins: "))
    bank = coins+bank
    o = input("would you like to take out 20p? Y or N")
    if o == "Y":
        bank = bank - 20
    else:
        print ("your balance is {0}".format(bank))
    op = input ("would you like to enter more coins? Y or N")
    if op == "Y":
        ATM()
    else:
        print ("goodbye")
        sys.exit()
    ATM()

Is there a way to make the local variable bank keep it's value from the end of the program? what would I have to switch bank = 0 for? EDIT: Found a solution, thank you all who offered some suggestions

oreo666
  • 1
  • 3
  • why don't you just store the local variable in a file? The program itself can - as with most other languages - not be changed (at least not easily) like this. – cleros Jan 21 '15 at 22:53
  • Syntax error in your code, the comparison should be `==` as in `if o == "Y":`, not single `=` – Anzel Jan 21 '15 at 22:59

3 Answers3

1

You can use pickle or you can save your data in to a .txt file;

#codes
....
with open("my_database.txt","a+") as f:
    f.write(str(bank))

So you could keep your bank data in a file. As you see we open the file in a+ mode which is not overwriting your file.

GLHF
  • 3,835
  • 10
  • 38
  • 83
0

You could pickle your data, read here: https://docs.python.org/2/library/pickle.html

Alternatively, since pickle is really more helpful for complex data structures, see this question on SO: Python, writing an integer to a '.txt' file

Using the example given in that link by Alex:

number = 1337

f = open('filename.txt', 'w')
f.write('%d' % number)
f.close()

This would mean you'd retrieve the value every time the program starts and save it on exit.

Community
  • 1
  • 1
Eithos
  • 2,421
  • 13
  • 13
  • This will overwrite the file if OP use this again. Also `with open()` method way better than `open()`. and you don't have to use `%d` , `str(number)` is ok. – GLHF Jan 21 '15 at 23:08
-1

You have two main options, make a main loop for your program instead of calling the same function, or you can pass arguments. It looks like you're new to programming, so I will explain them a bit for you:

While loops are basically blocks of code that will repeat over and over, until a certain condition is finished. They look like this:

exit = False
while exit != True:
  print "Hello!"
  exit = True

This loop in specific will only run once, since we set exit to True in the first iteration. However, if we took that line away, it would run forever until the program explodes or we close it out manually.

So one solution could be something like this:

def ATM():
  exit = False
  while exit != True:
    normal logic you are doing
    if op = 'N':
      exit = True
  sys.exit()

Another solution would be to pass an argument to ATM(). Passing arguments is basically a way to set up a variable for a function before it runs, which gives you the power to set things in outside functions. For instance:

def foo():
    my_var = "test"
    bar("test")
def bar(another_var)
  print another_var

This will print "test", even though the variable was defined in a function separate from the function which calls print. We're essentially passing our my_var variable to the bar() function, which sets it up for use for us. So you can do something like:

my_var = 123
def ATM(some_var):
  do stuff
  if exit == False:
    ATM(some_var)
  else:
    sys.exit()

Play with it first and let me know if you have any questions after.

  • @Micheal Wallace Thanks, this worked perfectly :) I moved bank so it became a global variable, and put bank within the def ATM brackets and it works. Anyone know what the words entered into the brackets are called/what the purpose of the brackets is? – oreo666 Jan 22 '15 at 17:46