首页 > 解决方案 > 我连接的这个 python 打印语句的最后一行有什么错误?

问题描述

我在最后一行找不到错误。我完全是这门语言的初学者。提前致谢。

ingredient_one = input("What is the first ingredient?: ")
ing_one_oz = int(input("How many ounces of this ingredient?: "))

ingredient_two = input("What is the second ingredient?: ")
ing_two_oz = int(input("How many ounces of this ingredient?: "))

ingredient_three = input("What is the third ingredient?: ")
ing_three_oz = int(input("How many ounces of this ingredient?: "))

servings_amt = int(input("How many servings would you like?: "))

ing_one_oz = ing_one_oz * servings_amt
ing_two_oz = ing_two_oz * servings_amt
ing_three_oz = ing_three_oz * servings_amt

print("In order to make " + str(servings_amt) + " servings of your recipe, you will need " + str(ing_one_oz) + " ounces of " /
+ str(ingredient_one) + ", " + str(ing_two_oz) + " ounces of " + str(ingredient_two) + ", and " + str(ing_three_oz) /
+ " ounces of " + str(ingredient_three))

我的输出TypeError: bad operand type for unary +: 'str'是最后一行。

标签: pythonprintingtypeerror

解决方案


对字符串执行操作时不能使用反斜杠。我相信这会解决你的问题。

ingredient_one = input("What is the first ingredient?: ")
ing_one_oz = int(input("How many ounces of this ingredient?: "))

ingredient_two = input("What is the second ingredient?: ")
ing_two_oz = int(input("How many ounces of this ingredient?: "))

ingredient_three = input("What is the third ingredient?: ")
ing_three_oz = int(input("How many ounces of this ingredient?: "))

servings_amt = int(input("How many servings would you like?: "))

ing_one_oz = ing_one_oz * servings_amt
ing_two_oz = ing_two_oz * servings_amt
ing_three_oz = ing_three_oz * servings_amt

print(
    f"In order to make {servings_amt} servings of your recipe, you will need {ing_one_oz} ounces of {ingredient_one}, {ing_two_oz} ounces of {ingredient_two}, {ing_three_oz} ounces of {ingredient_three}")

推荐阅读