#đź”’ Mimicking an ATM in Python
266 messages · Page 1 of 1 (latest)
@subtle sorrel
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.
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
Improper use of self. Need to be a class
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
balance is a local variable in a function so not available for reuse
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
the teacher told me they shouldnt be or i dont have to do that
when i sent her an email
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
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
I would start with the while loop first then create the function to support it
because i forget whether i do or not
__init__ is used in a class to initialize a new instance of that class
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
OK but now you need to call the functions based on project_read.txt
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
so how do you know what function to call?
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
You need to return balance if you want to keep track of it
Yes then you have to structyure you function call to recieve the return value
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?
should i put that in a while loop, or where should i put it?
In a while loop
with user imputs
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
work on what the imput gives
2 for withdraw
what about amount?
cant that be calculated within the call itself?
how do you know how much to pass to function?
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
you need 2 things transaction type and amount how will you get these?
transaction type will be obtained through the if/else statement
from input
aye, but the if/else statement is dependent on the users input is what im trying to say
thats my bad
so I use an atm I choose what transaction. Then if deposit or withdraw Iam asked for amount. Write code to get this input
give it a try.
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
write a while True loop to input transaction type then post and we can evaluate
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:
input needs to be inside the loop
for functcall?
yes
oh right, i forget you can just set a "while true" loop and it just runs
to exit on 4 - quit you can use break
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
dont need while functioncall
alright
the inner while functioncall would just return to while true
right
so then would it be better to reserve the "else" to be if the user enters something other than our specified numbers
sure to tidy up for bad input
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
oh right
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?
Your open file code needs work. Plus you need to change it from string to a float
yes i see it
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())
show me what you have for the while True now
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:
so if it is 1 or 2 you need an amount. A nested if is what works here
alright
I meant an input to get amount. Not the if
right
and then assign the amount within the function with the amount we assigned with input
current_balance = deposit(current_balance, deposit_amount)
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?
yes float
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)
looks right. now almost the same for a withdraw
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?
yes
and then i wrote
elif functcall == 3:
current_balance = inquiry(current_balance)
will that work?
Should
aye
then for the quit call, i have
elif functcall == 4:
current_balance = quit(current_balance)
f.close()
break
I learned a different way of reading?writing to files but you can see if that works
unkown input try again?
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
change the variable at the top to current_balance = float(f.read())
👍
all instances of it? like in the functions and such
or somewhere else
wait im dumb, lmao
i got you, my bad
no leave the balance in the funtions alone they are local variables and get destroyed when function exits
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'
post what you have
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
Thats why you test and test again
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
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
I thought that might not work
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
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
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
yes for the r. but than you are in read mode and you are done reading so f.close()
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
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
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.")
f = open("project_read".txt", "r")
current_balance = float(f.read())
f.close()
in quit
alright
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
Yes this replaces the previous balance
thats not what im supposed to do
what are your insturctions
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
Then choose a different name for saving the new balance txt file. everything else should be the same
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
a second file has to have a different name
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?
i dont know I am a sloppy typer where are you seeing one
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
post the code and i can test
👍
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.")
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.
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
line 1 add , "r"
alright
under def quit make it "w" and get rid of f.close() in the function looks like you close in the while true
👍
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
ok it was working for me
You are a good student and catch on quick. Nice helping you.
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
👍
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.