Rachel M.
asked 03/09/23I need help getting the correct output for my function. *Check comments for more info*
def run_sim(stops, boarding_rate, exiting_rate, passengers):
stop_count = 1
while stop_count <= stops:
if stop_count == stops:
boarding = 0
departing = passengers
else:
boarding = boarding_rate
departing = min(passengers, exiting_rate)
print(f"{stop_count:^10d}{passengers:^10d}{boarding:^10d}{departing:^10d}")
stop_count += 1
if stop_count <= stops:
passengers = calc_boarded(boarding, departing, passengers)
print(f"Final Stop: {passengers} on the bus")*line that needs removed*
print(run_sim(10, 15, 10, 5))
2 Answers By Expert Tutors

Bradley T. answered 03/10/23
Tutor for Python and High School and Middle School Math
Hi, I think the problem going on here is that your function run_sim is not returning anything, so when you go to print it in the final line, there is nothing to print. There are two ways to address this. The simple way is to not print when you run the function.
When we run it all the code inside executes, including the prints. Putting a print around this line would print the return value, which the function does not have one, so it prints None.
You could also return your final numbers and print it outside the function. That looks something like:
You don't even need to save the output to a variable, and run it inside the f-string. The first method is easier. I personally like my functions to always return something useful just in case I need it later in the code. It is up to you.
Note that we can't run the code because we don't have the calc_boarded function. So I wasn't able to check whether it gives the output that you need, but from what you explained in your comments I think this was the issue.
Rachel M.
Thank you so much! The first method is what I used so it would pass the required test methods. Have a good rest of your day/night!03/10/23
Khadeer P. answered 03/15/23
7996468281Data engineer and Trainer with expertise in Python
def run_sim(stops, boarding_rate, exiting_rate, passengers):
stop_count = 1
while stop_count <= stops:
if stop_count == stops:
boarding = 0
departing = passengers
else:
boarding = boarding_rate
departing = min(passengers, exiting_rate)
print(f"{stop_count:^10d}{passengers:^10d}{boarding:^10d}{departing:^10d}")
stop_count += 1
if stop_count <= stops:
passengers = calc_boarded(boarding, departing, passengers)
print(f"Final Stop: {passengers} on the bus")
run_sim(10, 15, 10, 5)
please find the corrected code.
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Rachel M.
The output for my function is a table. I need the function to only print out the table. But as it is currently it will print out the function plus the final number of passengers. If I get rid of that line it will return none after the output. If I use return "", it adds an empty line after the output.03/09/23