[Tutor] JES question concerning syntax

Peter Otten __peter__ at web.de
Tue Feb 4 11:11:16 CET 2014


Siobhan Wojcik wrote:

> Hello,
> 
> I've been having an issue with a program that I'm writing. The program
> works until it has to compute something:
> 
> def carPmt():
>   cost = requestInteger("How much does the car cost?")
>   downPmt = requestInteger("If there is a down payment, how much?")
>   tradeIn = requestInteger("If there is a trade-in, what is its value?")
>   years = requestInteger("How many years will you be paying?")
>   air = requestInteger("What is the interest rate?")
> 
>   payment = calculatePayment(cost,downPmt,tradeIn,years,air)
>   print "With a down payment of",downPmt," a trade-in value of",tradeIn,
>   print ", an annual interest rate of",air,", your new",cost, "car will
> cost",
>   print "you only",payment, "per month."
> 
> def calculatePayment(cost,downPmt,tradeIn,years,air):
>   carCostInitial = cost - downPmt
>   carCostIntermediate = carCostInitial - tradeIn
>   monIntRate = air/12
>   compoundingFactor = (1.0+monIntRate)**12.0/(1+monIntRate**12.0)-1.0
>   monthPmt = monIntRate*carCostIntermediate*compoundingFactor
>   return monthPmt
> 
> 
> 
> The first two sections work; the third one does not. When inputting random
> values, I get this: "With a down payment of 5000 ,a trade-in value of 7000
> ,an annual interest rate of 2 ,your new 25000 car will cost you only 0.0
> per month" Obviously I don't want my answer to be 0.0. I've had other
> people look at it (although they aren't well-versed in Python) and they
> couldn't help me isolate the problem.
> 
> Thanks a million.

Hint:

>>> 2/12
0

Potential fixes:

>>> 2/12.0
0.16666666666666666
>>> float(2)/12
0.16666666666666666

Or if you want to change the default behaviour for the whole module:

>>> from __future__ import division # at the beginning of the file
>>> 2/12
0.16666666666666666




More information about the Tutor mailing list