CS50P → 当我手动测试时,我的tip.py 文件工作正常。然而,当我使用CS50P的测试代码片段对其进行测试时,输出什么都没有("")

问题描述 投票:0回答:1

我目前正在参加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 ""
cs50
1个回答
0
投票

程序未按照规范运行。如果输入的内容分别没有

$
%
,它将产生正确的答案。 规范要求它们成为输入的一部分。

  • dollars_to_float
    ,应接受
    str
    作为输入(格式为
    $##.##
    ,其中每个 # 都是十进制数字),删除前导 $,并将金额作为浮点数返回。例如,输入 $50.00,它应该返回 50.0。
  • percent_to_float
    ,它应该接受
    str
    作为输入(格式为
    ##%
    ,其中每个 # 都是十进制数字),删除尾随 %,并以浮点形式返回百分比。例如,给定 15% 作为输入,它应该返回 0.15。
© www.soinside.com 2019 - 2024. All rights reserved.