Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with a banking Program
#1
Hello i am a 13 year old novice programer trying to learn and just mess around with programing. I am making a bank program and have been trying to fix this one bug that just doesnt want to work. its problem is the fact that it just isn't doing the math right. Lets say i will deposit 9999.2 just for testing cause thats what i use to test. And then lets say i withdraw 9999.1 the answer will always come out as -0.1000000000000000000003638. i have been trying to fix this for a long time and need some help at this point. I have been programming this on my school computer using the website Repl.it this is the program i have worked on.
bal = float(0)

while 1==1:
  float(bal)
  play_int = input("please enter wtd for withdraw and enter dep for deposit or enter bal to see your balance or enter end to end the program: ")
  print("")
  if play_int == "dep":
    while 1==1:
      try:
        dep = float(input("please enter the amount you would like to deposit: "))
        if dep < 0:
          print("")
          print("please enter a positive number")
          print("")
          break
        print("")
        bal = float(dep) + float(bal)
        print("you have deposited $"+ str(dep))
        print("")
        str(bal)
        print("$" + str(bal))
        print("")
        float(bal)
        break
      except ValueError:
        print("")
        print("you have entered a letter  or a decimal please enter a whole number")
        print("")
  elif play_int == "wtd":
    while 1==1:
      try:
       wtd = float(input("please enter the amount you would like to withdraw : "))
       if wtd < 0:
          print("")
          print("please enter a positive number")
          print("")
          break
       print("")
       bal = float(wtd) - float(bal)
       print("you have withdrawn " +  str(wtd))
       print("")
       print(str(bal))
       print("")
       float(bal)
       break
      except ValueError:
       print("")
       print("you have entered a letter or a decimal please enter a whole number")
       print("")
  elif play_int == "bal":
    str(bal)
    print("$" + str(bal))
    float(bal)
  elif play_int == "end":
    print("ending program")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print("program ended")
    break
  elif play_int == "crash":
    while 1==1:
      print("...................................................................................................................................................................")
~~ UwU
Reply
#2
Quote:bal = float(wtd) - float(bal)
to
bal = float(bal) - float(wtd)
you math is wrong, some other things I saw were your use of while 1==1, one will always equal one in this universe :0 nesting while loops is probably not the best practice you could try while True: once at the top and return False to end it. think about making functions for each transaction
def with_draw(amount_value):
    #balance-amount_value
or convert to guizero here
Reply
#3
It's an issue caused by floating point arithmetic
look also at https://docs.python.org/3/tutorial/floatingpoint.html

>>> deposit = 1000.2
>>> withdrawal = 1000.1
>>> balance = deposit - withdrawal
>>> balance
0.10000000000002274
>>> print('{:.2f}'.format(balance))
0.10
>>> print(f'{balance:.2f}')
0.10
>>>
one way to deal with it (sufficient at least for homework purposes) is to use string formatting - f-strings or str.format() method so that you represent the result, being float, up to 2 decimal places precision

In real life, when dealing with number representing money amounts one would make sure to always work with correct precision, so that there is no artefacts like extra cent due to rounding accumulated over time. One way is to use decimal module

>>> from decimal import Decimal
>>> deposit = Decimal('1000.2')
>>> withdrawal = Decimal('1000.1')
>>> balance = deposit - withdrawal
>>> balance
Decimal('0.1')
>>> print(balance)
0.1
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
In order to get easier to read and understand code you can improve usage of print function.

    print("ending program")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print("program ended")
Snippet above prints 13 rows. If this is your intention then you can do it much simpler:

>>> print('program ending', '\n.' * 11, '\nprogram ended')      # \n makes Python print on a new row
program ending 
.
.
.
.
.
.
.
.
.
.
. 
program ended
If you actually want to have dots on one row then you can write:

>>> print(f"program ending\n{'.' * 11}\nprogram ended")        # f denotes f-string and \n are newlines                                                                               
program ending
...........
program ended
Everywhere you use print("") you could use \n, so instead of:

print("")
print("please enter a positive number")
print("")
You can write:

print('\nplease enter a positive number\n')
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Hey guys thanks for all the help this is the finnished program

bal = float(0)

while 1==1:
  float(bal)
  play_int = input("please enter wtd for withdraw and enter dep for deposit or enter bal to see your balance or enter end to end the program: \n ")
  if play_int == "dep":
    while 1==1:
      try:
        dep = float(input("please enter the amount you would like to deposit: "))
        if dep < 0:
          print('\n please enter a positive number \n ')
          break
        bal = float(dep) + float(bal)
        print("you have deposited $"+ str(dep))
        print(f'{bal:.2f} \n')
        float(bal)
        break
      except ValueError:
        print(" you have entered a letter  or a decimal please enter a whole number \n")
  elif play_int == "wtd":
    while 1==1:
      try:
       wtd = float(input("please enter the amount you would like to withdraw : \n "))
       if wtd < 0:
          print("please enter a positive number \n ")
          break
       bal = float(bal) - float(wtd)
       print("you have withdrawn " +  str(wtd))
       print(f'{bal:.2f} \n')
       float(bal)
       break
      except ValueError:
       print("you have entered a letter or a decimal please enter a whole number \n ")
  elif play_int == "bal":
    str(bal)
    print(f'{bal:.2f}')
    float(bal)
  elif play_int == "end":
    print("ending program")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print(".")
    print("program ended")
    break
  elif play_int == "crash":
    while 1==1:
      print("...................................................................................................................................................................")
~~ UwU
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020