Problem with function output - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Problem with function output (/thread-22805.html) |
Problem with function output - slyfox - Nov-27-2019 Hi everyone, I'm working on a homework assignment to write a script to output the next major oil change and maintenance needed for a car. I've tried a ton of different ways of doing this, but can't get it to display the next 5 oil changes and next 3 major maintenance needs. I've managed an endless loop, I've managed to get it to repeat and display every number from the current mileage and 30000, but not what I need! As it is now, it is just getting the input, but not displaying any calculations. Any ideas? # This is a program that displays the maintenance schedule for a car # Defining global constants of oil change and major maintenance OIL_CHANGE_NEEDED = 3000 MAJOR_MAINTENANCE_NEEDED = 30000 # main function def main(): # get current mileage current_mileage = int(input("What is your car's current mileage?")) # display the oil change mileages display_oil_change(current_mileage) # display the major maintenance needed display_major_maintenance(current_mileage) #Display mile reading for next 5 oil changes def display_oil_change(mileage): end = int(input("How many oil changes would you like to know?")) for oil_change in range(mileage, end, OIL_CHANGE_NEEDED): print ("Your next oil change will be in", oil_change) # Display mile reading for next 3 major maintenance needs def display_major_maintenance(mileage): end = int(input("How many maintenance appointments would you like to know?")) for maintenance in range(mileage, end, MAJOR_MAINTENANCE_NEEDED): print ('Your next major maintenance will be in', maintenance) main() RE: Problem with function output - jefsummers - Nov-27-2019 Work through some examples. If I have 1500 miles and I want 3 oil changes, the for loop at line 17 would be over range (1500,3,3000). Not what you want. Technical detail - you sound like you want to give the mileages from the current mileage. That assumes you have your car in for oil and maintenance currently. I would want the program to not make that assumption, and give the 3000 mile intervals and 30000 mile intervals from brand new. If I have not changed the oil in a while, have 2500 miles on the car, and run the program to see when the next oil change would be, the answer you give would be 5500 miles, when I think you really want it to answer 3000 miles. By my logic, your next oil change would be in (int(mileage/3000)+1)*3000 miles. Increment that 1 to get your n change cycles. |