0

如何允许某人输入值前带有“$”的浮点整数。例如,他们可以输入“$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
4

1 回答 1

5

改变

mealPrice = int(float(input('Enter your meal price:')))

mealPrice = float(input('Enter your meal price:').lstrip("$"))

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

于 2013-01-30T20:10:45.930 回答