Python中的提示计算器功能

时间:2018-10-29 03:30:29

标签: python

我正在编写小费计算器功能,但在编写功能方面遇到了麻烦(通常)。这是我的开始。如果有人可以提供反馈,我将不胜感激。

def calculate_tip(check_amount, tip):

    total_bill = (input("What was the total of your bill with tax?"))
    tip_percent = (input("How much would you like to tip?"))
    tip_amount = total_bill * tip_percent
    tip_amount = float(total_tip)
    return tip_amount

calculate_tip(check_amount, tip)

2 个答案:

答案 0 :(得分:0)

#Define calculate_tip
def calculate_tip(check_amount, tip):
    tip_amount = total_bill * (tip_percent/100)
    return tip_amount

#Input amount
total_bill = (float(input("What was the total of your bill with tax?")))
tip_percent = (float(input("How much would you like to tip? (%)")))

#Print calculations
print("$",calculate_tip(total_bill,tip_percent))

当前您正在使用tip_amount = total_bill * tip_percent

这意味着您要乘以2的整数(除非用户输入10%为0.1)

相反,您应该使用tip_percent/100,以便用户可以输入整数,程序将可以按百分比读取数字。

此外,输入应放置在函数外部,以便可以根据输入更改参数。

答案 1 :(得分:0)

我认为这是使用功能的正确方法。您只需要将公式放入函数中即可。如果我错了,请纠正我。

def calculate_tip(check_amount, tip):

    total = check_amount * (tip/100) + check_amount 
    total = float(total)
    return float(total)

total_bill = (input("What was the total of your bill with tax? = "))
tip_percent = (input("How much would you like to tip (in percent %)? = "))

out = calculate_tip(float(total_bill), float(tip_percent))
print ("total is " + str(out))