Python Forum
Help on question about buses - 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: Help on question about buses (/thread-22126.html)



Help on question about buses - edwdas - Oct-30-2019

Hello,

So the purpose of this script is to take an input of passengers and calculate the cheapest cost of required buses. BigBus takes 48 passengers and small bus takes 10. E.g. 50 people would be 1 bigBus ($200 )and 1 smallBus ($95) = $295 and 20 passengers would just be 2 small and its associated cost.

Current script:

passengers = int(input("How many passengers? ")) 
bigBus = 0 
smallBus = 0  

remaining = passengers % 48  

while passengers > 0:  if passengers // 48 + int(remaining > 20):      
    bigBus += 1  
else:     
    (remaining + 9) // 10 * int(remaining <= 20)     
     smallBus += 1  

cost = (bigBus * 200) + (smallBus * 95)  
print("Hire", bigBus, "big buses and", smallBus, "small buses.")  
print("Cost =", cost) 
This script at the moment only asks for an input, then blanks out. Just a little lost on where i went wrong? i think it may be a math issue or a loop issue but I'm not sure


RE: Help on question about buses - jefsummers - Oct-30-2019

If I enter 100 for the number of passengers, do you ever decrement passengers or does it always stay 100 and always in your while loop?


RE: Help on question about buses - edwdas - Oct-31-2019

(Oct-30-2019, 11:58 PM)jefsummers Wrote: If I enter 100 for the number of passengers, do you ever decrement passengers or does it always stay 100 and always in your while loop?

I essentially just need it to 'decrease' into the buses e.g. 100 passengers will fill 2 big buses and 4 people on a small bus


RE: Help on question about buses - ichabod801 - Oct-31-2019

Yes but you never subtract from passengers, so it is always greater than 0 and the while loop runs forever. Also, I'm assuming your actual code is:

while passengers > 0:  
    if passengers // 48 + int(remaining > 20):      
        bigBus += 1  
    else:     
        (remaining + 9) // 10 * int(remaining <= 20)     
        smallBus += 1  
Note that the line after the else statement does nothing. It calculates a number, but you don't assign that to a variable, so it just disappears.


RE: Help on question about buses - edwdas - Oct-31-2019

Correct assumption, didn't notice sorry only been doing python for 2 days LOL!. so what would be the most appropriate fix for this?


RE: Help on question about buses - ichabod801 - Oct-31-2019

Each time you add a bus, subtract the capacity of that bus carries from passengers.