Basic Python repetition exercise

2024/7/6 21:28:54

I was trying to learn Python when I came upon this question. I am not asking for you to answer the question for me, I just need some help. Note: I am only allowed to use loops and if statements etc. Nothing ahead. I don't understand where I can use loops to create this program or the formulas needed.

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.

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 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:

count=0
total=0
gas=1.30
avgkm=20000normalcost=input("Please enter the cost of the non-hybrid vehicle: ")
hybridcost=input("Please enter the cost of the hybrid vehicle: ")
fueleff=input("Please enter the combined fuel effiency of both vehicles: ")    
Answer

I'll answer without providing too much code (after all, it's your homework, not mine).

First: Your question is about how many years the cost of a car (including fuel cost) will be equal (or similar) to the cost of the other car. So, you need three things: a year counter and two accrued costs:

i = 0              # The year counter
cost_a = price_a   # The accrued cost of car A: It starts with the cost of the car
cost_b = price_b   # The accrued cost of car B: It starts with the cost of the car

Then, you need to add the cost every year, so you need a loop that does three things: increment the year count, and add the costs to each car on every iteration. So, you need to ask yourself these questions:

  1. How much do you need to increment the costs for each car?
  2. What is your 'stop' criteria? In other words: When will you stop iterating?

For the increment, on each iteration you will need to do something like this:

i = i + 1    # Add a year
cost_a = cost_a + year_costs_a    # The increment of costs for car A during the year
cost_b = cost_b + year_costs_b    # The increment of costs for car B during the year

Finally: Inflation. Remember that inflation 'changes' the price every year, so:

price = price * (1 + inflation_rate)

Of course, you need to initialize the price with the appropriate value for this to make any sense.


All things said, to solve this problem (or any other programming problem), you need to 'split-and-divide' it in little tasks (or steps), and then try to put it all together.

https://en.xdnf.cn/q/120684.html

Related Q&A

Python 3.3 socket programming error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

ValueError: math domain error

I wrote this codedef partE():e = 3 * 10 // 3 + 10 % 3print("e).", e)partE()and python comes back with this error message when I try to run it. I do not understand why. Can someone please expl…

how to access objects in python with for loop in different files

This is my file1.json: {"count": 1,"next": null,"previous": null,"results": [{"id": 5883,"url": "https://some.api.com/api/ipam/ip-addres…

multiplicative digital root of a number using loops

I need to find the multiplicative digital root of a number in python using only loops.something that does the same as the below code but using loops:print("multiplicative digital root of a number …

How to access key values in a json files dictionaries with python

I have a script that pulls json data from an api, and I want it to then after pulling said data, decode and pick which tags to store into a db. Right now I just need to get the script to return specifi…

How to use sin(x) and cos(x) functions with eval

I need a program which can make graphs by matplotlib with functions I write in the console. But it doesnt work with trigonometric functions. The code I already wrote is:from numpy import linspace impo…

python - whats the difference between = and ==? [duplicate]

This question already has answers here:What do the symbols "=" and "==" mean in python? When is each used?(5 answers)Closed 5 years ago.I wonder know whats the difference between …

Sorting images by dates into a list from a dictionary

I know I asked this before, but Im still not sure why I just get an empty list when I test thisdef sorted_images(image_dict): (dict) -> list of strGiven an image dictionary return a list of the file…

multiple search and replace in python

I need to search in a parent folder all files that are config.xml and in those files replace one string in another. (from this-is to where-as)

how do I convert the first letter of every word in a list from upper case to lower case? [duplicate]

This question already has answers here:How to downcase the first character of a string?(9 answers)Closed 6 years ago.how do I convert the first letter of each of the below from upper case to lowere ca…