0

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. "
asked Apr 1, 2013 at 20:08
8
  • 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? Commented 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. Commented Apr 1, 2013 at 20:18
  • one of your classmates just asked for help with this question a few hours ago. Commented 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? Commented Apr 1, 2013 at 20:24
  • After I enter the four values, it says "Executing command. Please wait for result". Then nothing pops up. Commented Apr 1, 2013 at 20:26

2 Answers 2

1

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

answered Apr 1, 2013 at 20:22
Sign up to request clarification or add additional context in comments.

9 Comments

The question is asking me to output how many years of ownership it will take for the two cars to equal in cost. So wouldn't the it still be hybrid_cost==standard_cost? Also what do you mean with 2)
Also even if I use hybrid_cost <= standard_cost, it still will not work.
The problem is, it will probably not be an exact amount of years. Say at year 5 the hybrid costs 5000 and the standard 4900. At year 6 it could be hybrid 5500 and standard 6000. So it will never be the exact same number.
But can't it be like 1.2 years? With decimals and such
in 2) I mean that you don't need those lines at all. The while loop will exit as soon as hybrid_cost == standard cost.
|
0

Your loop will stop only if standard_cost equals hybrid_cost exactly. You should stop the loop if hybrid_cost <= standard_cost.

answered Apr 1, 2013 at 20:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.