0

I'm writing an ATM program for school. I've been doing a lot of research trying to figure out why when my numbers are like 100.00 they become 100.0 when cast as strings. I've tried many different things but always get errors.

import sys

#account balance
acct_bal = float(500.25)
deposit_amount = float(0.00)
balance = float(0.00)
withdrawal_amount = float(0.00)

#<--------functions go here-------------------->
#printbalance function, choice B
def account_balance(acct_bal):
    print("Your current balance:")
    print(acct_bal)

#deposit function, choice D
def deposit(deposit_amount, balance):
    print("Deposit was $" + str(float(deposit_amount)) + ", current balance is $" + str(float(balance)))
    #print("Deposit was $" + "{:,.2f}".format(deposit_amount) + ", current balance is $" + "{:,.2f}".format(balance))
    #This one causes an error
    #Traceback (most recent call last):
    #File "G:/SNHU/1 - CURRENT/IT-140 Introduction to Scripting Python (10-30-17 to 12-24-17)/Module 5/ATM", line 29, in <module>
    #deposit(deposit_amount, balance)
    #File "G:/SNHU/1 - CURRENT/IT-140 Introduction to Scripting Python (10-30-17 to 12-24-17)/Module 5/ATM", line 17, in deposit
    #print("Deposit was $" + "{:,.2f}".format(deposit_amount) + ", current balance is $" + "{:,.2f}".format(balance))
    #ValueError: Unknown format code 'f' for object of type 'str'

#withdraw function, choice W
def withdrawal(withdrawal_amount, balance):
    print("Withdrawal amount was $" + str(float(withdrawal_amount)) + ", current balance is $" + str(float(balance)))

#User Input goes here, use if/else conditional statement to call function based on user input
userchoice = input("What would you like to do?\n")

if (userchoice == "D"):
    deposit_amount = input("How much would you like to deposit today?\n")
    balance = acct_bal + float(deposit_amount)
    deposit(deposit_amount, balance)
elif (userchoice == "B"):
    account_balance(acct_bal)
else:
    withdrawal_amount = input("How much would you like to withdraw?\n")
    balance = acct_bal - float(withdrawal_amount)
    withdrawal(withdrawal_amount, balance)

This is the output I get:

What would you like to do?
W
How much would you like to withdraw?
100
Withdrawal amount was $100.0, current balance is $400.25

Should be >

Withdrawal amount was $100.00, current balance is $400.25

Process finished with exit code 0

yash
  • 1,357
  • 2
  • 23
  • 34
James H
  • 11
  • 1
  • 3
  • 2
    Possible duplicate of [Limiting floats to two decimal points](https://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points) – mkrieger1 Dec 03 '17 at 01:47
  • Use those commented out lines with `{:,.2f}.format(...)`. – mkrieger1 Dec 03 '17 at 01:50
  • And pass a number, not a string, to the `deposit` function. (You need to convert the result of `input` to a number.) – mkrieger1 Dec 03 '17 at 01:52
  • 1
    Or, `print("Deposit was $%.2f, current balance is $%.2f") % (deposit_amount, balance))`. – dkato Dec 03 '17 at 01:52
  • I tried the {:,.2f}".format(deposit_amount) which gave the errors listed under the def deposit() function. – James H Dec 03 '17 at 02:13
  • dkato, I tried yours and get a different error: – James H Dec 03 '17 at 02:13
  • Traceback (most recent call last): Deposit was $%.2f, current balance is $%.2f File "G:/SNHU/1 - CURRENT/IT-140 Introduction to Scripting Python (10-30-17 to 12-24-17)/Module 5/ATM", line 42, in deposit(deposit_amount, balance) File "G:/SNHU/1 - CURRENT/IT-140 Introduction to Scripting Python (10-30-17 to 12-24-17)/Module 5/ATM", line 20, in deposit print("Deposit was $%.2f, current balance is $%.2f") % (deposit_amount, balance) TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' – James H Dec 03 '17 at 02:14
  • Try `print("Deposit was $%.2f, current balance is $%.2f" % (deposit_amount, balance))` – yash Dec 03 '17 at 02:18
  • 1
    The easiest way would be to use `format` function of `str` – yash Dec 03 '17 at 02:23

1 Answers1

2

if you are using python 3.x then there is a simple way to go about it.

value = 100.000

#the .2f specifys the number of trailing values after the decimal you want
final = "{:.2f}".format(value)

print(final)
#output:100.00
#NOTE: it will be in string
#if you want more formatting options you can check the python docs
Nex_Lite
  • 358
  • 2
  • 7
  • I get these errors: Traceback (most recent call last): File "G:/SNHU/1 - CURRENT/IT-140 Introduction to Scripting Python (10-30-17 to 12-24-17)/Module 5/ATM", line 44, in deposit(deposit_amount, balance) File "G:/SNHU/1 - CURRENT/IT-140 Introduction to Scripting Python (10-30-17 to 12-24-17)/Module 5/ATM", line 20, in deposit deposit_amount = "{:,.2f}".format(deposit_amount) ValueError: Unknown format code 'f' for object of type 'str' – James H Dec 03 '17 at 02:47
  • why is there a comma there "{:','.2f}" – Nex_Lite Dec 03 '17 at 03:09