Python双参数不起作用

时间:2016-03-05 17:01:26

标签: python

所以我有一个程序,我希望得到提示,使用两个参数。我首先提示用户输入一个userBill,这是账单成本的金额,然后我建议用户输入15%或30%的小费,但让他们输入十进制格式。我的问题是当我调用方法Problem3()时,它在获取userBill和tipRate的值后也调用了problem3中的方法提示,但我不断收到此错误。对不起,感到困惑。

>>> def Problem3():
    userBill = input("Enter the total of your bill at the restaurant.")
    tipRate = input("Would you like to tip .15 or .30 of your bill?")
    userBill = int(userBill)
    tipRate = float(tipRate)
    def Tip(userBill, tipRate):
        tipRate1 = .15
        tipRate2 = .30
        userTip = 0

        if tipRate == tipRate1:
            userTip = tipRate1 * userBill
        else:
            userTip = tipRate2 * userBill
        print("Your tip is " + userTip + " since you've want to tip at")
    Tip(userBill, tipRate)

>>> Problem3()
Enter the total of your bill at the restaurant.100
Would you like to tip .15 or .30 of your bill?.15
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    Problem3()
  File "<pyshell#22>", line 16, in Problem3
    Tip(userBill, tipRate)
  File "<pyshell#22>", line 15, in Tip
    print("Your tip is " + userTip + " since you've want to tip at")
TypeError: Can't convert 'float' object to str implicitly
>>> 

1 个答案:

答案 0 :(得分:2)

您真的不需要Tip()函数中的条件和局部变量:

def Tip(userBill, tipRate):
    userTip = tipRate * userBill
    print("Your tip is {0} since you've want to tip at {1}".format(userTip, tipRate))
Tip(userBill, tipRate)

你真的不需要内部功能:

def Problem3():
    userBill = input("Enter the total of your bill at the restaurant.")
    tipRate = input("Would you like to tip .15 or .30 of your bill?")
    userBill = int(userBill)
    tipRate = float(tipRate)
    userTip = tipRate * userBill
    print("Your tip is {0} since you've want to tip at {1}".format(userTip, tipRate))