Reputation: 51
I am writing a program using functions where it gets the total number of passengers, the total for the fare, and the profit, which is just half of the total fare. The problem I am having is that it runs Please enter the number of passengers for bus1 :
Please enter the number of passengers for bus2 :
Please enter the number of passengers for bus3 :
***********
two times then it gives me the output. I just need it to run once and then give the output. Here's my code:
def CalculateBus1():
bus1 = input("Please enter the number of passengers for bus1 :")
return bus1
def CalculateBus2():
bus2 = input("Please enter the number of passengers for bus2 :")
return bus2
def CalculateBus3():
bus3 = input("Please enter the number of passengers for bus3 :")
return bus3
bus1 = CalculateBus1()
bus2 = CalculateBus2()
bus3 = CalculateBus3()
total = (bus1+bus2+bus3)
fare = float(total*2.50)
profit = float(fare/2)
print("***********")
print"There are total {} passengers from three buses." .format(total)
print"The total fare earned is: ${}" .format(fare)
print"The net profit is: ${}" .format(profit)
print("***********")
Upvotes: 2
Views: 68
Reputation: 440
You are calling your functions twice, both here:
CalculateBus1()
CalculateBus2()
CalculateBus3()
and here:
total = (CalculateBus1()+CalculateBus2()+CalculateBus3())
Either remove the first three calls, or do
passengersBus1 = CalculateBus1()
passengersBus2 = CalculateBus2()
passengersBus3 = CalculateBus3()
total = passengersBus1 + passengersBus2 + passengersBus3
After seeing your edit, I think you are a bit confused about how calling functions works. See here for more details
Upvotes: 2
Reputation: 539
You do not need to call Calculate Bus1,2,3 by itself first. Remove the code:
CalculateBus1()
CalculateBus2()
CalculateBus3()
What you are doing is prompting the user for input and returning the input in each of those functions. You are not saving them to variables. This means that if you run calculateBus1()
, it will prompt you for input and then discard the return. All you need is
total = (CalculateBus1()+CalculateBus2()+CalculateBus3())
What this code does it it runs your CalculateBus functions which in turn prompt the user for input and then sums them.
Upvotes: 4