0

I am doing a project for school and at the end I am to summarize the customer(user) bill.

If I remove the '%.2f' then I do not receive an error, but the number displayed goes on and on and on. How do I display only the last 2 decimal places?

Code I am using:

print('Amount Due:        $' + '%.2f' % baseCharge)

Output I receive:

    print('Amount Due:        $' + '%.2f' % baseCharge)
TypeError: a float is required

Entire code:


rentalCode = input("(B)udget, (D)aily, or (W)eekly rental?\n")

weeksRented = 0
daysRented = 0
budgetCharge = 40
dailyCharge = 60
weeklyCharge = 190


if rentalCode == 'B' or rentalCode == 'D':
  daysRented = input("Number of Days Rented:") #Prompt --> "Number of Days Rented:"
else:
    weeksRented = input("Number of Weeks Rented:\n") #Prompt --> "Number of Weeks Rented:"

odoStart = int(input("Starting Odometer Reading:\n"))
odoEnd = int(input("Ending Odometer Reading:\n"))
totalMiles = odoEnd - odoStart

print(rentalCode) # Displays Chosen Rental Code
if rentalCode == 'B' or rentalCode == 'D': # Decides which option has been input and displays the output
  print(daysRented)
else:
  print(weeksRented)

baseCharge = 0
if rentalCode == 'B':
  baseCharge = daysRented * budgetCharge
elif rentalCode == 'D':
  baseCharge = daysRented * dailyCharge
elif rentalCode == 'W':
  baseCharge = weeksRented * weeklyCharge



#Print odoStart, odoEnd and totalMiles
print(odoStart)
print(odoEnd)
print(totalMiles)
# Calculate Charges 2

totalCharge = 0
if rentalCode == 'B':
  totalCharge = baseCharge + str(totalMiles * .25)
elif rentalCode == 'D':
  averageDayMiles = int(totalMiles) / int(daysRented)
  if averageDayMiles <= 100:
    totalCharge = baseCharge
  else:
    extraMiles = totalMiles - 100
    totalCharge = baseCharge + str(extraMiles * .25)
elif rentalCode == 'W':
  averageWeekMiles = int(totalMiles) / int(weeksRented)
  if averageWeekMiles <= 900:
    totalCharge = baseCharge
  else:
    totalCharge = baseCharge + str(weeksRented * 100)

print(totalCharge)

if rentalCode == "W":
    rentalPeriod = weeksRented
else:
    rentalPeriod = daysRented

print(rentalPeriod)

if rentalCode == "B":
    baseCharge = budgetCharge * rentalPeriod
elif rentalCode == "D":
    baseCharge = dailyCharge * rentalPeriod
else:
    baseCharge = weeklyCharge * rentalPeriod




print('Customer Summary')
print('Rental Code:       ' + rentalCode)
print('Rental Period:     ' + rentalPeriod)
print('Starting Odometer: ' + str(odoStart))
print('Ending Odometer:   ' + str(odoEnd))
print('Miles Driven:      ' + str(totalMiles))
print('Amount Due:        $' + '%.2f' % baseCharge)

Again, if that %.2f is in place, I receive the output error, but if I take it out, I'll receive a large number. Example: instead of 951.65 i'll get 951.6555555555555555555 etc

Brandon Weeks
  • 69
  • 1
  • 2
  • 11
  • 1
    `baseCharge` is obviously a string. You're not actually doing any math in most of the places you think you are, you're just doing string replication, along the lines of `"5" * 10 == "5555555555"`. – jasonharper Mar 25 '19 at 04:39
  • Start your calculations with floats: `0.0`, `40.0`... – Klaus D. Mar 25 '19 at 04:39
  • A good [mcve] would have only the *shortest possible code* that others can run without changes to reproduce your problem. We don't need the whole program if you can have just one line that assigns a variable, and a second line that tries to print it. – Charles Duffy Mar 25 '19 at 04:44
  • @KlausD. So if I set my basecharge = 40.00 that would have been better? – Brandon Weeks Mar 25 '19 at 05:01

1 Answers1

0
rentalCode = input("(B)udget, (D)aily, or (W)eekly rental?\n")

weeksRented = 0
daysRented = 0
budgetCharge = 40
dailyCharge = 60
weeklyCharge = 190


if rentalCode == 'B' or rentalCode == 'D':
  daysRented = int(input("Number of Days Rented:")) #Prompt --> "Number of Days Rented:"
else:
    weeksRented = int(input("Number of Weeks Rented:\n")) #Prompt --> "Number of Weeks Rented:"

odoStart = int(input("Starting Odometer Reading:\n"))
odoEnd = int(input("Ending Odometer Reading:\n"))
totalMiles = odoEnd - odoStart

print(rentalCode) # Displays Chosen Rental Code
if rentalCode == 'B' or rentalCode == 'D': # Decides which option has been input and displays the output
  print(daysRented)
else:
  print(weeksRented)

baseCharge = 0
if rentalCode == 'B':
  baseCharge = daysRented * budgetCharge
elif rentalCode == 'D':
  baseCharge = daysRented * dailyCharge
elif rentalCode == 'W':
  baseCharge = weeksRented * weeklyCharge



#Print odoStart, odoEnd and totalMiles
print(odoStart)
print(odoEnd)
print(totalMiles)
# Calculate Charges 2

totalCharge = 0
if rentalCode == 'B':
  totalCharge = baseCharge + (totalMiles * .25)
elif rentalCode == 'D':
  averageDayMiles = int(totalMiles) / int(daysRented)
  if averageDayMiles <= 100:
    totalCharge = baseCharge
  else:
    extraMiles = totalMiles - 100
    totalCharge = baseCharge + (extraMiles * .25)
elif rentalCode == 'W':
  averageWeekMiles = int(totalMiles) / int(weeksRented)
  if averageWeekMiles <= 900:
    totalCharge = baseCharge
  else:
    totalCharge = baseCharge + (weeksRented * 100)

print(totalCharge)

if rentalCode == "W":
    rentalPeriod = weeksRented
else:
    rentalPeriod = daysRented

print(rentalPeriod)

if rentalCode == "B":
    baseCharge = budgetCharge * rentalPeriod
elif rentalCode == "D":
    baseCharge = dailyCharge * rentalPeriod
else:
    baseCharge = weeklyCharge * rentalPeriod




print('Customer Summary')
print('Rental Code:       ' , rentalCode)
print('Rental Period:     ' , rentalPeriod)
print('Starting Odometer: ' , str(odoStart))
print('Ending Odometer:   ' , str(odoEnd))
print('Miles Driven:      ' , str(totalMiles))
print('Amount Due:        $' + '%.2f' % baseCharge)

Here, you go. The daysRented and weeksRented can be taken as integers. Also, you can concatenate only strings and not int to string. Another thing, %.2f works for real numbers not strings.
Here is a sample output

(B)udget, (D)aily, or (W)eekly rental?
B
Number of Days Rented:2
Starting Odometer Reading:
100
Ending Odometer Reading:
150
B
2
100
150
50
92.5
2
Customer Summary
Rental Code:        B
Rental Period:      2
Starting Odometer:  100
Ending Odometer:    150
Miles Driven:       50
Amount Due:        $80.00
Anurag A S
  • 725
  • 10
  • 23
  • Thank you!!!! I see what you did. When you say i cant concatenate int to string, i shouldn't have put the str in front of (totalMiles * .25) or (extraMiles * .25). And the input for days and weeks should be as an int. Again, thank you, I was about to lose my mind. – Brandon Weeks Mar 25 '19 at 04:47