【发布时间】:2019-09-05 18:45:27
【问题描述】:
我的作业是编写一个计算出租车费用的程序。但是,出租车收取 2.00 英镑的基本票价,前五英里每英里每英里 2.00 英镑,之后每英里每英里 1.00 英镑。并且老师给了我们一个提示说我们可以把计算票价的部分写成一个函数
这就是我所做的... 没有错误,但唯一的问题是我的程序在输入任何距离时都不起作用
user_fare = int(input('Please enter the distance '))
if user_fare == 0:
print('2')
elif user_fare > 0 and user_fare < 5:
def distance_into_money(fare):
return ((user_fare*2)+2)
print(distance_into_money)
elif user_fare > 5:
def distance_into_money(fare):
return ((user_fare*2)+1)
print(distance_into_money)
else:
print('Error')
我预计 1 英里的输出为“4.00 英镑”,6 英里的输出为“13.00 英镑”,-1 英里的输出为“错误”,但我的程序没有输出
【问题讨论】:
-
如何确定?正如老师所说,编写一个以距离为参数并返回票价的函数。然后,(手动)计算几个有代表性的距离(例如 0、1、2.5、4、5、6、7、10、50 英里)的正确值,并验证所有这些距离的结果是否正确。另外,谷歌
unittest。 -
这两个
elif语句定义了函数,但它们不运行它们。这些定义有其自身的问题。return后面的print无法执行。
标签: python python-3.x python-2.7 ipython