#đź”’ Mimicking an ATM in Python

266 messages · Page 1 of 1 (latest)

subtle sorrel
#

I need to use functions in order to mimic the functions of an ATM machine. The functions I need to define are for deposit, withdraw, balance inquiry and quit. The default amount of money is given from an external file.

rugged iglooBOT
#

@subtle sorrel

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

subtle sorrel
#

The code I have rn is this:

#

f = open("project_read.txt")

def init(self.balance):
self.balance = float(f.read())

def deposit(self.balance, amount):
self.balance += amount
print("Your new balance is: $", self.balance)

def withdraw(self.balance, amount):
if (self.balance - amount) < 0:
print("Insufficient funds.")
else:
self.balance -= amount
if self.balance < 100:
print("Warning! Your balance is currently under $100!")
print("Your new balance is: $", self.balance)
else:
print("Your new balance is: $", self.balance)

def quit(self.balance):
f = open("project.txt")
f.write(self.balance)
print("Thank you for choosing our ATM.")

#

but im sure there's a lot here that I can clip out, and even after this, i need to be able to probably make a while loop that keeps going until the user inputs something that calls the quit function and stops the program or whatever

#

im sure i probably dont need some of the "self" parts or whatever bc im not defining a class here

quiet shard
#

Improper use of self. Need to be a class

subtle sorrel
#

just making sure

#

and i wasnt sure whether or not i needed to keep the "amount" variables in the definitions, but i dont see why i wouldnt need them

#

bc you can define the variable with the call of the function or whatever, iirc

quiet shard
#

balance is a local variable in a function so not available for reuse

blissful laurel
#
def deposit(self.balance, amount):
```should be
```py
def deposit(self, amount):
#

and same for the other

#

also yeah these should be inside a class

subtle sorrel
#

the teacher told me they shouldnt be or i dont have to do that

#

when i sent her an email

blissful laurel
#

she might've said that you don't have to use a class (idk), but what you have here are methods that belong in a class body

#

you can't just delete the class ATM: and keep everything else the same

subtle sorrel
#

and thats probably bc i was confused bc i kept thinking that every function needs to have "self" within the parentheses

#

when thats probably not the case

#

but if its not, would it be possible for me to like

#

get rid of the init function and have the balance be defined outside of the definitions

#

or do i need to keep the init function

quiet shard
#

I would start with the while loop first then create the function to support it

subtle sorrel
#

because i forget whether i do or not

blissful laurel
#

__init__ is used in a class to initialize a new instance of that class

subtle sorrel
#

cool

#

f = open("project_read.txt")

balance = float(f.read())

def deposit(balance, amount):
balance += amount
print("Your new balance is: $", balance)

def withdraw(balance, amount):
if (balance - amount) < 0:
print("Insufficient funds.")
else:
balance -= amount
if balance < 100:
print("Warning! Your balance is currently under $100!")
print("Your new balance is: $", balance)
else:
print("Your new balance is: $", balance)

def quit(balance):
f = open("project.txt")
f.write(balance)
print("Thank you for choosing our ATM.")

#

does this look any better

quiet shard
#

OK but now you need to call the functions based on project_read.txt

subtle sorrel
#

right

#

that file's just used to give the "account" its initial value, there's only one number in there

#

and im pretty sure i know how to potentially call the functions, probably would use a while loop

#

but what i want to do is like

quiet shard
#

so how do you know what function to call?

subtle sorrel
#

i want to use a while loop, first off

#

so the user can go through the list and be asked what they want to do over and over until they enter the option that calls the quit function

#

but the user will have to input like a number or string in order to get the thing they want

quiet shard
#

You need to return balance if you want to keep track of it

subtle sorrel
#

yea

#

so should i just write that under some of those functions?

#

return balance?

quiet shard
#

Yes then you have to structyure you function call to recieve the return value

subtle sorrel
#

ok, cool

#

so, like....

#

f = open("project_read.txt")

balance = float(f.read())

def deposit(balance, amount):
balance += amount
print("Your new balance is: $", balance)
return balance

def withdraw(balance, amount):
if (balance - amount) < 0:
print("Insufficient funds.")
else:
balance -= amount
if balance < 100:
print("Warning! Your balance is currently under $100!")
print("Your new balance is: $", balance)
return balance
else:
print("Your new balance is: $", balance)
return balance

def quit(balance):
f = open("project.txt")
f.write(balance)
print("Thank you for choosing our ATM.")

#

something more like this, yea?

quiet shard
#

current_balance = deposit(current_balance, deposit_amount)

#

for your function call

subtle sorrel
#

should i put that in a while loop, or where should i put it?

quiet shard
#

In a while loop

subtle sorrel
#

cool

#

for that loop, i was thinking of using like

quiet shard
#

with user imputs

subtle sorrel
#

an if/else embedded into it

#

maybe

#

bc that way, i can run or call the functions with the user's input string or number

#

so like

#

if the user inputs 1 it runs the deposit

quiet shard
#

work on what the imput gives

subtle sorrel
#

2 for withdraw

quiet shard
#

what about amount?

subtle sorrel
#

cant that be calculated within the call itself?

quiet shard
#

how do you know how much to pass to function?

subtle sorrel
#

through the user's input, but i thought that like

#

you could do something like deposit(input())

#

but i could be very wrong

#

bc of how the calls/functions are formatted

#

with the balance being the first value in the parentheses

quiet shard
#

you need 2 things transaction type and amount how will you get these?

subtle sorrel
#

transaction type will be obtained through the if/else statement

quiet shard
#

from input

subtle sorrel
#

aye, but the if/else statement is dependent on the users input is what im trying to say

#

thats my bad

quiet shard
#

so I use an atm I choose what transaction. Then if deposit or withdraw Iam asked for amount. Write code to get this input

subtle sorrel
#

aye

#

more or less what im trying to get

#

my only problem then is like

quiet shard
#

give it a try.

subtle sorrel
#

how i am able to assign the value of amount in the functions with the amount that the user gets

#

or do i just like

#

set amount equal to input and that works

quiet shard
#

write a while True loop to input transaction type then post and we can evaluate

subtle sorrel
#

gotcha, already on it

#

also thanks for the help btw

#

f = open("project_read.txt")

balance = float(f.read())

def deposit(balance, amount):
balance += amount
print("Your new balance is: $", balance)
return balance

def withdraw(balance, amount):
if (balance - amount) < 0:
print("Insufficient funds.")
else:
balance -= amount
if balance < 100:
print("Warning! Your balance is currently under $100!")
print("Your new balance is: $", balance)
return balance
else:
print("Your new balance is: $", balance)
return balance

def quit(balance):
f = open("project.txt")
f.write(balance)
print("Thank you for choosing our ATM.")

functcall = input('Thank you for choosing our ATM! To deposit, please enter 1. To withdraw, enter 2. To print a Balance Inquiry, enter 3. Enter 4 to quit.\n')

while functcall != 4:
if functcall == 1:

elif functcall == 2:

elif functcall == 3:
quiet shard
#

input needs to be inside the loop

subtle sorrel
#

for functcall?

quiet shard
#

yes

subtle sorrel
#

oh right, i forget you can just set a "while true" loop and it just runs

quiet shard
#

to exit on 4 - quit you can use break

subtle sorrel
#

right

#

f = open("project_read.txt")

balance = float(f.read())

def deposit(balance, amount):
balance += amount
print("Your new balance is: $", balance)
return balance

def withdraw(balance, amount):
if (balance - amount) < 0:
print("Insufficient funds.")
else:
balance -= amount
if balance < 100:
print("Warning! Your balance is currently under $100!")
print("Your new balance is: $", balance)
return balance
else:
print("Your new balance is: $", balance)
return balance

def quit(balance):
f = open("project.txt")
f.write(balance)
print("Thank you for choosing our ATM.")

while True:
functcall = input('Thank you for choosing our ATM! To deposit, please enter 1. To withdraw, enter 2. To print a Balance Inquiry, enter 3. Enter 4 to quit.\n')

while functcall != 4:
    if functcall == 1:

    elif functcall == 2:

    elif functcall == 3:
#

so more like this

#

imma throw the else function in there, too, dw

#

just making sure its correct so far

quiet shard
#

dont need while functioncall

subtle sorrel
#

alright

quiet shard
#

the inner while functioncall would just return to while true

subtle sorrel
#

right

#

so then would it be better to reserve the "else" to be if the user enters something other than our specified numbers

quiet shard
#

sure to tidy up for bad input

subtle sorrel
#

aye, thats what i was thinking

#

bc the only numbers we need are 1, 2, 3 and 4

quiet shard
#

still neee if functioncall == 4:

#

break

subtle sorrel
#

yea, i just added it in, dw

#

the functcall wont break immediately once 4 is called btw, because thats what the quit function is for

#

has to output the account value and the message before breaking

quiet shard
#

oh right

subtle sorrel
#

but thats all good, bc we can still break the function once that call is run iirc

#

we'll also need to close the file, but it'd work if we did that within that elif, right?

quiet shard
#

Your open file code needs work. Plus you need to change it from string to a float

subtle sorrel
#

i thought i already changed it

#

thats why balance is equal to float(f.read())

quiet shard
#

yes i see it

subtle sorrel
#

but i couldve missed a little something there, so if i need to change it i can

#

i just remember that before i structured everything with the functions like you told me, i tried it with if/else statements, which was not what i was supposed to do, but it worked

#

and i had balance or some other thing equal to float(f.read())

quiet shard
#

show me what you have for the while True now

subtle sorrel
#

its more or less unchanged, i havent filled the things in with the calls yet

#

while True:
functcall = input('Thank you for choosing our ATM! To deposit, please enter 1. To withdraw, enter 2. To print a Balance Inquiry, enter 3. Enter 4 to quit.\n')

if functcall == 1:

elif functcall == 2:

elif functcall == 3:

elif functcall == 4:

else:
quiet shard
#

so if it is 1 or 2 you need an amount. A nested if is what works here

subtle sorrel
#

alright

quiet shard
#

I meant an input to get amount. Not the if

subtle sorrel
#

right

#

and then assign the amount within the function with the amount we assigned with input

quiet shard
#

current_balance = deposit(current_balance, deposit_amount)

subtle sorrel
#

should i also set the input to float, or does it not matter bc the balance is a float and it'll convert it anyway?

quiet shard
#

yes float

subtle sorrel
#

gotcha

#

ok, this is what i have for that call:

#

if functcall == 1:
deposit_amount = float(input("Please enter how much you would like to deposit.\n"))
current_balance = deposit(current_balance, deposit_amount)

quiet shard
#

looks right. now almost the same for a withdraw

subtle sorrel
#

right

#

elif functcall == 2:
withdraw_amount = float(input("Please enter the amount you would like to withdraw.\n"))
current_balance = withdraw(current_balance, withdraw)

#

for 3, all we have to do is print the balance

#

shit, i didnt make a balance inquiry function

#

def inquiry(balance):
print("Your balance is: $", balance)
return balance

#

that should work, yea?

quiet shard
#

yes

subtle sorrel
#

and then i wrote

#

elif functcall == 3:
current_balance = inquiry(current_balance)

#

will that work?

quiet shard
#

Should

subtle sorrel
#

aye

#

then for the quit call, i have

#

elif functcall == 4:
current_balance = quit(current_balance)
f.close()
break

quiet shard
#

I learned a different way of reading?writing to files but you can see if that works

subtle sorrel
#

aye

#

and then for the else function...

quiet shard
#

unkown input try again?

subtle sorrel
#

yeye

#

more or less

#

now lemme test this out

#

oh wait, i should probably have it change the functcall to an integer if we're gonna be using numbers

#

damn, says current balance isnt defined :/

#

i called for deposit and entered a number, but thats the error i got

quiet shard
#

change the variable at the top to current_balance = float(f.read())

subtle sorrel
#

👍

#

all instances of it? like in the functions and such

#

or somewhere else

#

wait im dumb, lmao

#

i got you, my bad

quiet shard
#

no leave the balance in the funtions alone they are local variables and get destroyed when function exits

subtle sorrel
#

aight, looks like its working now

#

gonna put in a few more things, then we'll see if the quit function branch works

#

mmm, get an error when i try to run the withdraw function

#

TypeError: unsupported operand type(s) for -: 'float' and 'function'

quiet shard
#

post what you have

subtle sorrel
#

it's an error within the function

#

f = open("project_read.txt")

current_balance = float(f.read())

def deposit(balance, amount):
balance += amount
print("Your new balance is: $", balance)
return balance

def withdraw(balance, amount):
if (balance - amount) < 0:
print("Insufficient funds.")
else:
balance -= amount
if balance < 100:
print("Warning! Your balance is currently under $100!")
print("Your new balance is: $", balance)
return balance
else:
print("Your new balance is: $", balance)
return balance

def inquiry(balance):
print("Your balance is: $", balance)
return balance

def quit(balance):
f = open("project.txt")
f.write(balance)
print("Thank you for choosing our ATM.")

while True:
functcall = int(input('Thank you for choosing our ATM! To deposit, please enter 1. To withdraw, enter 2. To print a Balance Inquiry, enter 3. Enter 4 to quit.\n'))

if functcall == 1:
    deposit_amount = float(input("Please enter how much you would like to deposit.\n"))
    current_balance = deposit(current_balance, deposit_amount)

elif functcall == 2:
    withdraw_amount = float(input("Please enter the amount you would like to withdraw.\n"))
    current_balance = withdraw(current_balance, withdraw)

elif functcall == 3:
    current_balance = inquiry(current_balance)

elif functcall == 4:
    current_balance = quit(current_balance)
    f.close()
    break

else:
    print("You didn't input any of the listed numbers! Please try again.")
#

i was trying to see if i could get it to output the warning if i got the balance to be less than 100 but i got that error instead

quiet shard
#

you have withdraw needs to be withdraw_amount

#

in you call

subtle sorrel
#

damn youre right, lmao

#

idk how i missed that

quiet shard
#

Thats why you test and test again

subtle sorrel
#

aye

#

im just a bit reckless with some of these things, cos i havent had a particularly fun time with the class, lmao

#

but with all that being said, you have helped a lot and i appreciate your time

#

i do get an error with the quit function tho, cos it says the directory for the file doesnt exist

#

but im supposed to be making the file with the code

quiet shard
#

With any project think of the steps you need to do. You worked on the functions first but maybe look at the while true first next time

subtle sorrel
#

aye

#

but how should i fix the quit function

quiet shard
#

I thought that might not work

subtle sorrel
#

aye

#

the printing files and such or whatever can be fussy sometimes

#

we had some issues with it in class, as a matter of fact

#

but thats the only thing left to fix

quiet shard
#

read this for the proper way to handle a file. https://www.w3schools.com/python/python_file_open.asp

#

You need to open the file for r , get data ,then close.

#

when ready to write open the file for w , put data, then close

subtle sorrel
#

i gotcha

#

so i need to throw the r in there next to the filename in the function

#

and then it should be able to allow for writing once it is called

#

or am i wrong

quiet shard
#

yes for the r. but than you are in read mode and you are done reading so f.close()

subtle sorrel
#

actually, should i change the value from 'f' to 'g' for that function? bc i already have 'f' defined as the value used to open the first file

#

the one with the initial balance

#

or does it not matter

quiet shard
#

When ready to write open in w mode write data then f.close()

#

it can be f because you closed the read so it is not in use

subtle sorrel
#

alright, lemme change the code real quick in that case and you can lmk what you think

#

f = open("project_read.txt")

current_balance = float(f.read())

def deposit(balance, amount):
balance += amount
print("Your new balance is: $", balance)
return balance

def withdraw(balance, amount):
if (balance - amount) < 0:
print("Insufficient funds.")
else:
balance -= amount
if balance < 100:
print("Warning! Your balance is currently under $100!")
print("Your new balance is: $", balance)
return balance
else:
print("Your new balance is: $", balance)
return balance

def inquiry(balance):
print("Your balance is: $", balance)
return balance

def quit(balance):
f = open("project.txt", "r")
f.close()
f.write(balance)
print("Thank you for choosing our ATM.")

while True:
functcall = int(input('Thank you for choosing our ATM! To deposit, please enter 1. To withdraw, enter 2. To print a Balance Inquiry, enter 3. Enter 4 to quit.\n'))

if functcall == 1:
    deposit_amount = float(input("Please enter how much you would like to deposit.\n"))
    current_balance = deposit(current_balance, deposit_amount)

elif functcall == 2:
    withdraw_amount = float(input("Please enter the amount you would like to withdraw.\n"))
    current_balance = withdraw(current_balance, withdraw_amount)

elif functcall == 3:
    current_balance = inquiry(current_balance)

elif functcall == 4:
    current_balance = quit(current_balance)
    f.close()
    break

else:
    print("You didn't input any of the listed numbers! Please try again.")
quiet shard
#

f = open("project_read".txt", "r")

#

current_balance = float(f.read())

#

f.close()

#

in quit

subtle sorrel
#

alright

quiet shard
#

f = open("project_read".txt", "w")

#

f.write(str(current_balance)

#

f.close()

subtle sorrel
#

are you sure? that text file already exists, the point of the quit function is to make a new txt file that has the new balance

#

it looks like that would write on the existing file

#

unless im missing something

quiet shard
#

Yes this replaces the previous balance

subtle sorrel
#

thats not what im supposed to do

quiet shard
#

what are your insturctions

subtle sorrel
#

The instructions are to take a value from an already existing txt file and set that as the account’s initial balance, have the user input what they want to do (deposit, withdraw, print balance) until they use the quit function, which would create an entirely new text file with a different name that holds the new value

#

Not replacing the original one

#

So by the end you should have 2 txt files with different values

#

When you started with 1

#

Sorry if that wasn’t as clear before

quiet shard
#

Then choose a different name for saving the new balance txt file. everything else should be the same

subtle sorrel
#

the teacher requires it to be named like that

#

Originally it has my last name in there, but I took that out

#

at least when I pasted it here

#

It’s still there in the actual code

quiet shard
#

a second file has to have a different name

subtle sorrel
#

I thought it did. Did I not have them named differently? Hold on

#

yeah, first one is project_read while the next is just project

#

Is there an error in there?

quiet shard
#

i dont know I am a sloppy typer where are you seeing one

subtle sorrel
#

I’m not seeing one

#

It could just be python being fussy

#

Cos like, even last class people were having trouble

#

Shit wouldn’t print or even if a file was printed, python couldn’t find the file

quiet shard
#

post the code and i can test

subtle sorrel
#

👍

#

f = open("project_read.txt")

current_balance = float(f.read())

f.close()

def deposit(balance, amount):
balance += amount
print("Your new balance is: $", balance)
return balance

def withdraw(balance, amount):
if (balance - amount) < 0:
print("Insufficient funds.")
else:
balance -= amount
if balance < 100:
print("Warning! Your balance is currently under $100!")
print("Your new balance is: $", balance)
return balance
else:
print("Your new balance is: $", balance)
return balance

def inquiry(balance):
print("Your balance is: $", balance)
return balance

def quit(balance):
f = open("project.txt", "r")
f.close()
f.write(balance)
print("Thank you for choosing our ATM.")

while True:
functcall = int(input('Thank you for choosing our ATM! To deposit, please enter 1. To withdraw, enter 2. To print a Balance Inquiry, enter 3. Enter 4 to quit.\n'))

if functcall == 1:
    deposit_amount = float(input("Please enter how much you would like to deposit.\n"))
    current_balance = deposit(current_balance, deposit_amount)

elif functcall == 2:
    withdraw_amount = float(input("Please enter the amount you would like to withdraw.\n"))
    current_balance = withdraw(current_balance, withdraw_amount)

elif functcall == 3:
    current_balance = inquiry(current_balance)

elif functcall == 4:
    current_balance = quit(current_balance)
    f.close()
    break

else:
    print("You didn't input any of the listed numbers! Please try again.")
rugged iglooBOT
#

Hey @subtle sorrel!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
subtle sorrel
#

you should probably make a project_read txt file first, too

#

the value we had in the file was 5000, but you can put whatever value you want in there

quiet shard
#

line 1 add , "r"

subtle sorrel
#

alright

quiet shard
#

under def quit make it "w" and get rid of f.close() in the function looks like you close in the while true

subtle sorrel
#

👍

#

now i get a different error, line 38

#

TypeError: write() argument must be str, not float

#

current_balance = quit(current_balance)

#

so do i just change current_balance to a string?

#

ah, looks like that might be it

#

lemme try it

#

it works :D

#

thanks for the help, i really appreciate it

quiet shard
#

ok it was working for me

subtle sorrel
#

aye, i got it to work as well

#

printed the new value and everything

quiet shard
#

You are a good student and catch on quick. Nice helping you.

subtle sorrel
#

i hope so, bc i feel like i havent learned all that much in her class when she's teaching LMAO

#

much appreciated, though. have a good one

quiet shard
#

👍

rugged iglooBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.