3 Replies - 2012 Views - Last Post: 22 April 2013 - 02:43 AM Rate Topic: -----

#1 mwtack567  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-April 13

writing a simple atm program

Posted 19 April 2013 - 05:31 PM

I am trying to write a simple program as an atm that lets the user withdraw or deposit. The problem I have is getting the deposit amount to work. here is the code i am using, please tell me what i am doing wrong.
        dpst = input("HOW MUCH WOULD YOU LIKE TO DEPOSIT? ")

        dpst_bal = str(new_bal + dpst)

        print ("BEGINNING BALANCE: $" + str(balance))
        print ("NEW BALANCE: $" + str(dpst_bal))


It will print the beginning balance, but where I want the new balance with the deposit I get new_bal + dpst

This post has been edited by atraub: 19 April 2013 - 08:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: writing a simple atm program

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: writing a simple atm program

Posted 19 April 2013 - 08:22 PM

If I'm understanding you correctly, you problem is that if your starting balance is 5 and you deposit 6 you end up with 56 rather than 11. This means that rather than doing addition, you're concatenating strings.

start_balance = 5 #notice, no quotes
deposit = int(input("How much you would like to deposit: ")) #notice I surround it with int(), this turns it into an integer, you can use float() if you want to account for cents

print ("BEGINNING BALANCE: $" + str(start_balance))
print ("NEW BALANCE: $" + str(start_balance+deposit))




Try that.

This post has been edited by atraub: 19 April 2013 - 08:22 PM

Was This Post Helpful? 1
  • +
  • -

#3 mwtack567  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-April 13

Re: writing a simple atm program

Posted 20 April 2013 - 08:16 AM

That was exactly what was happening. Thank you very much.
Was This Post Helpful? 0
  • +
  • -

#4 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

Re: writing a simple atm program

Posted 22 April 2013 - 02:43 AM

View Postmwtack567, on 19 April 2013 - 05:31 PM, said:

I am trying to write a simple program as an atm that lets the user withdraw or deposit. The problem I have is getting the deposit amount to work. here is the code i am using, please tell me what i am doing wrong.
        dpst = input("HOW MUCH WOULD YOU LIKE TO DEPOSIT? ")

        dpst_bal = str(new_bal + dpst)

        print ("BEGINNING BALANCE: $" + str(balance))
        print ("NEW BALANCE: $" + str(dpst_bal))


It will print the beginning balance, but where I want the new balance with the deposit I get new_bal + dpst


So basically what you are doing wrong is that your dpst_balance it is set to string variable. Although we want to make sure that we are dealing with float (double) numbers. So a quick fix is to make sure that you organize your code a bit cleaner and make sure to convert into float.

# Declare your new balance and deposit variable, and set to your intial value.
# Since Python won't know what variable you are talking about.
new_bal = 0.0
dpst_bal = 0.0

# Creates a dpst variable that converts float.
# Note: you cannot enter string, it will give you a ValueError.
dpst = float(input("HOW MUCH WOULD YOU LIKE TO DEPOSIT?"))

# Make sure to declare a variable for new_bal, or also it will give error
new_bal = dpst_bal + dpst

# Make sure you re-use your variable.
# Note: I saw your program that you created new variable
print("BEGINNING BALANCE: $", dpst_bal)
print("NEW BALANCE: $", new_bal)


Few things to keep in note is:
- Don't create unnecessary variables, as I see you create a lot of variables.
variable

- Concatenate your variables only if is between a sentence, it is more efficiency and neater.
new_value = 10
print("This is when you should concatenate " +str(new_value) + "-value.")


Pretty much your code doesn't seem bad for first programmer, just keep in track of your variables :), hope that helped.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1