Python:在float整数前面允许"$"符号

Jor*_*lop 0 python calculator

如何允许某人输入值前面带有"$"的浮点整数.例如,他们可以输入"4.25美元",也可以输入"4.25".

此外,当我在计算器中输入"4.35"时,提示出现"0.6".在我在家里拥有的计算器上它是0.6525.我如何得到完整的答案?

input ('Please Enter to begin')

while True:
    print('This calculator will display the tip you owe for your meal price.')
    mealPrice = int(float(input('Enter your meal price:')))
    asw = mealPrice * 0.15
    print('The tip you owe is: $',asw)

    endProgram = input ('Do you want to restart the program?')

    if endProgram in ('no', 'No', 'NO', 'false', 'False', 'FALSE'):
        break
Run Code Online (Sandbox Code Playgroud)

Dav*_*son 5

更改

mealPrice = int(float(input('Enter your meal price:')))
Run Code Online (Sandbox Code Playgroud)

mealPrice = float(input('Enter your meal price:').lstrip("$"))
Run Code Online (Sandbox Code Playgroud)

lstrip("$")从字符串的左侧删除任何出现的给定字符.你还需要删除int,将价格截断到最接近的美元(这也是你有时得到错误答案的原因).