-1

I am trying to get back into coding with python since I have a basic cert in it. I figured I would try doing a simple ATM machine to start. However once I set my code up and try running it it doesn't print out the balance even though the balance variable is assigned a value. Can someone please guide me in what I am doing wrong with my code.

balance = 2000
withdraw = 0
deposit = 0


print("Welcome to the Heart Cold ATM")
print("Select 1 to View Currrent Balance")
print("Select 2 for Depsoit")
print("Select 3 for Withdraw")
print("Select 4 to Quit")
userInput = input("Please put the option that you want execute: ")

if userInput == 1:
    print("Your current balance is " + balance)
elif userInput == 2:
    depsoit = input("Enter the amount you would like to deposit: ")
    balance += deposit
    print(balance)
elif userInput == 3:
    withdraw = input("How much would you like to withdraw: ")
    balance -= withdraw
    if withdraw > balance: 
        print("Insufficient fund unavilable to withdraw from your account. Please re-enter a different amount")
    print(balance)
else: 
    ExitNow
rioV8
  • 24,506
  • 3
  • 32
  • 49
Blank
  • 1
  • 1
    `input()` returns a string so you need to convert it to int, otherwise you can't compare: `userInput = int(input("Please put the option that you want execute: "))` – NotAName May 18 '22 at 01:35
  • https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – thebjorn May 18 '22 at 01:42
  • that would question the validity of the `basic cert` – rioV8 May 18 '22 at 06:08
  • @rioV8 I got the cert like two years ago and haven't touch python since. I am currently a data engineer so I do not touch code very often. I want to get back into so that I can switch careers. – Blank May 19 '22 at 02:05
  • And what is wrong with reading the doc if a function is not doing what you think it should do, and have you thought about using a debugger – rioV8 May 19 '22 at 06:31

2 Answers2

0
balance = 2000
withdraw = 0
deposit = 0


print("Welcome to the Heart Cold ATM")
print("Select 1 to View Currrent Balance")
print("Select 2 for Depsoit")
print("Select 3 for Withdraw")
print("Select 4 to Quit")
userInput = input("Please put the option that you want execute: ")
if userInput == "1":
    print("Your current balance is ", balance)
elif userInput == "2":
    deposit = int(input("Enter the amount you would like to deposit: "))
    balance += deposit
    print(balance)
elif userInput == "3":
    withdraw = int(input("How much would you like to withdraw: "))
    balance -= withdraw
    if withdraw > balance: 
        print("Insufficient fund unavilable to withdraw from your account. Please re-enter a different amount")
    print(balance)
else: 
    exit()

your mistakes are print("somthing" + somthing) you should do that print("somthing" , somthing) and the output of input is a string you should do that

int(input("somthing"))
Dizaster
  • 19
  • 5
0

Hey first time answering a question on here, but the answer isn't anything too crazy. You can't concatenate a string with an integer, so you need to cast the variable 'balance' to a string, then you can concatenate and print out the entire string.

Not super necessary, but I also added a while loop that will help keep your ATM transaction running until you want it to end by typing the number 4. The way you had it previously set up would end the program if you typed anything other than '1' '2' or '3', while this way it will only end on a '4'.

Another issue was when you are trying to deposit and withdraw, you would also need to cast the input you are receiving from the user as an int, so you can actually compute the new balance total.

balance = 2000
withdraw = 0
deposit = 0
run = True

print("Welcome to the Heart Cold ATM")
print("Select 1 to View Currrent Balance")
print("Select 2 for Depsoit")
print("Select 3 for Withdraw")
print("Select 4 to Quit")

while run == True:
    userInput = input("Please put the option that you want execute: ")
    if userInput == '1':
        print("Your current balance is:" + str(balance))
    elif userInput == '2':
        deposit = int(input("Enter the amount you would like to deposit: "))
        balance = balance + deposit
        print(balance)
    elif userInput == '3':
        withdraw = int(input("How much would you like to withdraw: "))
        balance = balance - withdraw
        if withdraw > balance:
            print("Insufficient fund unavailable to withdraw from your account. Please re-enter a different amount")
        print(balance)
    elif userInput == '4':
        print("loop is now ending")
        run = False
    else:
        print("You have entered an invalid response.")
dahar72
  • 1
  • 2
  • Hi Thanks for answering. After fixing the issue I had I put it in a While loop to keep it going until the user is ready to quit out of it. I also did the casting for the other variables as well once I saw some other errors I had. – Blank May 19 '22 at 02:03