r/pythonhelp • • Dec 07 '20

Python Homework Help Please

The code I need to write needs to meet these criteria:

Regular servicing will keep your vehicle more reliable, reducing the chance of breakdowns, inconvenience and unnecessary expenses. In general, there are two types of service you need to

perform:

a. a minor service every 6000 miles

b. a major service every 12000 miles

Write a Python program that prompts the user to enter the miles traveled, and then calculates and displays how many miles are left until the next service, as well as the type of the next service.

Ex:

Enter miles traveled: 21600

Your car needs a major service in 2400 miles.

3 Upvotes

2 comments sorted by

View all comments

2

u/sentles Dec 07 '20

The % operator will return the remainder of the division between its two operands. For instance, 5%3 will return 2, because 5/3 is 1 and has a remainder of 2. The remainder of a division shows how much "extra" there is in the dividend that does not "fit" in the divisor. To understand this, take 21 / 5. 21 / 5 = 4, remainder is 1. What does this mean? It means that the number 5 can fit 4 times in 21. The "extra" part of 21, the remaining 1, cannot fit any 5s in it. Therefore, that is the remainder. The modulo operator (%) simply returns that for any division.

If you needed service every 5 miles and your car had 21 miles, you'd need service in 4 miles. To calculate this 4, we can take the divisor (5, which is the mile mark for service) and subtract that "extra" part (the remainder) from it. 21 % 5 = 1, so to find the miles that are left until the next service, we simply do 5 - (21 % 5) = 5 - 1 = 4 miles.

In your case, you need to find how many miles are left up until the next 6000, or the next 12000 mile mark. This is the same as the example above. To find how many miles are left until the next 12000 mile mark, you simply need to subtract the remainder from the divisor. In this case, the dividend is the mileage of your car and the divisor is 12000. Below is a function that will calculate this for a given mileage:

def next12k(m):
    return 12000 - m % 12000

You can do the exact same thing for 6000:

def next6k(m):
    return 6000 - m % 6000

Now all that's left is figuring out whether we need a major or minor service next. To do this, we observe the following: when a minor service is needed next, the miles needed up until the next 6k mark (so the return value of next6k) are less than the miles needed up until the next 12k mark (so the return value of next12k). This makes sense; if your car has 5000 miles, it will need a minor service in 1000 miles (next6k(5000) = 1000) but a major one in 7000 (next12k(5000) = 7000). Therefore, when next6k(mileage) < next12k(mileage), you need a minor service next in next6k(mileage) miles.

When you need a major service next, you'll notice that the values of next6k(mileage) and next12k(mileage) are equal. This is because 12000 is a multiple of 6000. So when you're at, for example, 11000, you need 1000 for the next 12k mark, but also 1000 for the next 6k mark. This is because, after 1000, you'll be at the second 6k mark. Therefore, a way to detect whether we need a major service next, is to check if next6k(mileage) == next12k(mileage).

All that's left now is for you to accept an integer from the user representing mileage, and do what I explained with it.

2

u/Educational-Green-60 Dec 07 '20

OMG! Thank you so much. That makes so much sense. Thank you again! Have a great day! Thank you!