Skip to content
Advertisement

Why is my round function not working when I have a float in Python? I tried many methods online, but none work [closed]

I was working on a project where I could find out a users balance with a monthly payment, interest and term of the payment. But when I look through the code my round function shows an error. I looked online and found different methods like formatting and such but none seemed to work. It all seems correct, I asked a few classmates and they can’t figure out the problem either. Help is appreciated. If you want me to specify more you can comment and I can get to it as soon as possible.

Link for the file used in the program: https://vsbworld-my.sharepoint.com/:x:/g/personal/1253540_learn_vsb_bc_ca/ESbQS3z58CdBspGmKN74IDABH-mlT7B7tN_ZFuFtq77fpQ?e=f15c8c

""" 

Created on Mon Nov 28 10:19:18 2022 

Ask how much they can afford to spend per month, then give a list of cars they can buy. 

@author: ... 

""" 

#List Library
count = 0 
price = 0
make = [] 
model = [] 
year = [] 
fuel = [] 
hp = [] 
cylinders = [] 
transmission = [] 
driveTrain = [] 
doors = [] 
category = [] 
size = [] 
body = [] 
highwayMPG = [] 
cityMPG = [] 
popularity = [] 
msrp = [] 

#File Search 

with open("car data.csv") as file: 

    #Set information into seperate lists 

    for line in file: 
        carInformation = line.split(",") 
        make.append(carInformation[0]) 
        model.append(carInformation[1]) 
        year.append(carInformation[2]) 
        fuel.append(carInformation[3]) 
        hp.append(carInformation[4]) 
        cylinders.append(carInformation[5]) 
        transmission.append(carInformation[6]) 
        driveTrain.append(carInformation[7]) 
        doors.append(carInformation[8]) 
        category.append(carInformation[9]) 
        size.append(carInformation[10]) 
        body.append(carInformation[11]) 
        highwayMPG.append(carInformation[12]) 
        cityMPG.append(carInformation[13]) 
        popularity.append(carInformation[14]) 
        msrp.append(carInformation[-1]) 

#Remove the first element of the list 

make.remove(make[0]) 
model.remove(model[0]) 
year.remove(year[0]) 
fuel.remove(fuel[0]) 
hp.remove(hp[0]) 
cylinders.remove(cylinders[0]) 
transmission.remove(transmission[0]) 
driveTrain.remove(driveTrain[0]) 
doors.remove(doors[0]) 
category.remove(category[0]) 
size.remove(size[0]) 
body.remove(body[0]) 
highwayMPG.remove(highwayMPG[0]) 
cityMPG.remove(cityMPG[0]) 
popularity.remove(popularity[0]) 
msrp.remove(msrp[0]) 

# introduction 

print("Hello, What is your name?") 
name = input() 

print("Hello " + name + ", I am Car Bot, I can help you select a car that fits all your needs.") 
print("Would you like to purchase a car? (y) Yes, (n) No. ") 
purchase = input().lower().strip(" .,!?abcdefghijklmopqrstuvwxz") 
if purchase == "n": 
    print("Ok, Thank You, for using Car Bot") 

# when someone wants to buy a car 

if purchase == "y": 

    print("OK! LETS GET STARTED!!") 
    print("Would you like to Finance or Finance?") 
    purchase_selection = input().lower() 

    # if they want to Pay by Finance 

    if purchase_selection == "finance":
        payment = float(input("How much can you put down monthly? "))
        term = float(input("How long is the term you are willing to pay the loan? "))
        interest = float(input("What is the interest rate in your area? "))
        r = interest
        n = 12.0
        
        # payment calulation and print
        price = payment * (1.0 - (1.0 + r/n)**-(n * term) * (n/r)
        price = round(price, 2)
        # list of cars that can be purchased
        for cost in msrp:
            if float(cost) <= price:
                print("ID(" + str(count) + ")" + make[count] + " " + model[count] + " $" + str(cost)
            count+= 1
        
        # for more info about the cars

Advertisement

Answer

Here is a version of your code that works. Your CSV file has quoted fields with embedded commas, so your “split” was not sufficient. I’ve changed this to use the csv module, since it can handle quoting. I’ve also converted the MSRP column to float right away.

Your interest/payment computation was wrong. I’ve fixed that. It seems kind of silly to ask “do you want to buy a car”, since they would not have run the program if they didn’t.

import sys
import csv

#File Search 

with open("car data.csv") as file: 
    carinfo =  list(csv.DictReader(file))

for c in carinfo:
    c['MSRP'] = float(c['MSRP'])

# Introduction 

name = input("Hello, What is your name? ") 

print("Hello " + name + ", I am Car Bot, I can help you select a car that fits all your needs.") 
purchase = input("Would you like to purchase a car? (y) Yes, (n) No. ").lower()

if purchase == "n": 
    print("Ok, Thank You, for using Car Bot") 
    sys.exit(0)

# when someone wants to buy a car 

print("OK! LETS GET STARTED!!") 
purchase_selection = input("Would you like to Finance or Finance?").lower()

# if they want to Pay by Finance 

if purchase_selection == "finance":
    payment = float(input("How much can you put down monthly? "))
    term = float(input("How long is the term you are willing to pay the loan? (months)"))
    interest = float(input("What is the interest rate in your area? "))
    r = interest/1200.0

    # payment calulation and print
    r1 = (1.0 + r)**term
    price = payment * (r1 - 1) / (r * r1)
    print(f"You can afford a car worth ${price:.2f}")

    # list of cars that can be purchased
    for count,car in enumerate(carinfo):
        if car['MSRP'] <= price:
            print(f"ID({count}) {car['Make']} {car['Model']} ${car['MSRP']:.2f}")
    
    # for more info about the cars
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement