我目前正在参加CS50P。 我正在解决问题集 0(函数、变量),即最后一个练习,即提示计算器。 我已经完成任务了。当我手动测试时效果很好。然而,当我使用 CS50P 的测试代码片段对其进行测试时,没有任何输出。
这是我的代码:
def main():
dollars = dollars_to_float(input("How much was the meal? $"))
print(dollars)
percent = percent_to_float(input("What percentage would you like to tip? %"))
print(percent)
tip = dollars * percent
print(f"Leave ${tip:.2f}")
def dollars_to_float(d):
return float(d)
def percent_to_float(p):
return float(p) / 100
main()
**测试结果:**
:) tip.py exists
:( input of $50.00 and 15% yields $7.50
expected "Leave $7.50\n", not ""
:( input of $100.00 and 18% yields $18.00
expected "Leave $18.00\n...", not ""
:( input of $15.00 and 25% yields $3.75
expected "Leave $3.75\n", not ""
程序未按照规范运行。如果输入的内容分别没有
$
和 %
,它将产生正确的答案。 规范要求它们成为输入的一部分。
,应接受dollars_to_float
作为输入(格式为str
,其中每个 # 都是十进制数字),删除前导 $,并将金额作为浮点数返回。例如,输入 $50.00,它应该返回 50.0。$##.##
,它应该接受percent_to_float
作为输入(格式为str
,其中每个 # 都是十进制数字),删除尾随 %,并以浮点形式返回百分比。例如,给定 15% 作为输入,它应该返回 0.15。##%