Python Forum
Problem with function output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with function output
#1
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()
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The "in" function, weird output extricate 3 1,986 Jun-07-2020, 05:28 AM
Last Post: extricate
  function output student8 1 2,399 Oct-12-2017, 05:30 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020