Search code examples
cs50

CS50P → My tip.py file works fine when I test it manually. However, when I test it using the CS50P's testing code snippet, the output is nothing ("")


I'm currently participating in CS50P. I am solving the Problem Set 0 (Functions, Variables), namely the last exercise i.e. TIP CALCULATOR. I have completed the task. It works fine when I test it manually. However, when I test it using the CS50P's testing code snippet, the output is nothing.

Here is my code:

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()

**Test results: **

:) 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 ""

Solution

  • The program does not work according to the specification. It will produce the correct answer if the inputs are entered without the $ and % respectively. The spec requires them to be part of the input.

    • dollars_to_float, which should accept a str as input (formatted as $##.##, wherein each # is a decimal digit), remove the leading $, and return the amount as a float. For instance, given $50.00 as input, it should return 50.0.
    • percent_to_float, which should accept a str as input (formatted as ##%, wherein each # is a decimal digit), remove the trailing %, and return the percentage as a float. For instance, given 15% as input, it should return 0.15.