Python Forum
Help on question about buses
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help on question about buses
#1
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
Reply
#2
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?
Reply
#3
(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
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Correct assumption, didn't notice sorry only been doing python for 2 days LOL!. so what would be the most appropriate fix for this?
Reply
#6
Each time you add a bus, subtract the capacity of that bus carries from passengers.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

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