【发布时间】:2018-11-20 06:02:07
【问题描述】:
我正在编写一个程序,它使用函数获取乘客总数、票价总额和利润,这只是总票价的一半。我遇到的问题是它运行Please enter the number of passengers for bus1 :
Please enter the number of passengers for bus2 :
Please enter the number of passengers for bus3 :
*********** 两次然后它给了我输出。我只需要它运行一次然后给出输出。这是我的代码:
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("***********")
【问题讨论】:
-
将从
CalculateBuses 获得的值分配给一些变量并对变量求和。 -
请注意,您似乎将
class与function混淆了。当您在 python 中使用关键字def时,这意味着您正在defining 一个函数。类有点复杂,是使用class关键字创建的。
标签: python