I needed some help with this program. I already tried something but it doesn't seem to work. When I click run, nothing happens. I think there may be something wrong with my loop but I am not too sure. Also I do not know how to add the inflation in. Any help is appreciated!
Additional info: Your parents need to buy a new vehicle and they are trying to decide whether to purchase a hybrid or not. Hybrid vehicles produce less CO2 emissions and have better fuel efficiency compared to their non-hybrid counterpart. However, hybrid vehicles also cost a lot more money than their non-hybrid version. Help your parents make a decision as to which type of vehicle to buy (strictly in terms of the financial cost and not taking into account the environmental benefits). The typical family drives 20,000 kms each year and gas currently costs 1ドル.30/litre.
Question:
Allow the user to enter the cost of the hybrid and non-hybrid vehicle along with the
combined fuel efficiency of those vehicles. Also, allow the user to enter the average
amount of kilometers they drive each year (note: the average is 20000 km/year). Then o
output how many years of ownership it will take for the two cars to equal in cost.
Assume that the price of gas stays the same at 1ドル.30/litre.
Obviously, the cost of gas will increase each year (this is called inflation).
Incorporate into your calculation the idea that gas prices will rise by 3% each year
(i.e. annual inflation rate is 3%).
This is what I have so far:
i=0 # The year counter
gas=1.30
avgkm=20000
inflation=1.03
standard_cost=0
hybrid_cost=0
standard_cost=input("Enter the price of the non-hybrid car: ")
hybrid_cost=input("Enter the price of the hybrid car: ")
fueleff1=input("Enter litres/100km for the standard car: ")
fueleff2=input("Enter litres/100km for the hybrid car: ")
standardyear=avgkm/100 * fueleff1 * gas + standard_cost
hybridyear=avgkm/100 * fueleff2 * gas + hybrid_cost
while standard_cost != hybrid_cost:
i += 1 # Add a year
standard_cost += standardyear
hybrid_cost += hybridyear
if hybrid_cost==standard_cost:
break
print "It will take",i,"years. "
-
Works on my machine (rather, it at least produces output). Are you using an IDE? Have you tried running it straight from the command line?Kevin– Kevin2013年04月01日 20:15:09 +00:00Commented Apr 1, 2013 at 20:15
-
I am using WingIDE. I have tried running it straight from the command line and it still won't work. There is no error or anything, it just stops and does not display anything. Other code works though. Its something wrong with this code. Maybe an infinite loop? I'm not sure.Joshua Cunningham– Joshua Cunningham2013年04月01日 20:18:14 +00:00Commented Apr 1, 2013 at 20:18
-
one of your classmates just asked for help with this question a few hours ago.MattDMo– MattDMo2013年04月01日 20:19:27 +00:00Commented Apr 1, 2013 at 20:19
-
To be entirely clear: are you starting the program and it immediately does not display anything? Or, are you starting the program, entering values for the four user input prompts, and then it does not display anything?Kevin– Kevin2013年04月01日 20:24:48 +00:00Commented Apr 1, 2013 at 20:24
-
After I enter the four values, it says "Executing command. Please wait for result". Then nothing pops up.Joshua Cunningham– Joshua Cunningham2013年04月01日 20:26:02 +00:00Commented Apr 1, 2013 at 20:26
2 Answers 2
I see a couple of issues:
1) Your loop will only exit if standard_cost is exactly equal to hybrid_cost you might want to try breaking out of your loop when hybrid_cost <= standard_cost
2) the lines
if hybrid_cost==standard_cost:
break
in your while loop are redundant with the conditional statement in the while
9 Comments
Your loop will stop only if standard_cost equals hybrid_cost exactly. You should stop the loop if hybrid_cost <= standard_cost.