python - 我连接的这个 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'
是最后一行。
解决方案
对字符串执行操作时不能使用反斜杠。我相信这会解决你的问题。
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}")
推荐阅读
- ubuntu-18.04 - 带有 ROS 包的“gpg:密钥服务器接收失败:连接被拒绝”
- python-3.x - 如何以 24 小时为间隔绘制熊猫数据框?(多个地块)
- android - 如何在 Android recyclerVIew 中单击项目时保存状态
- ruby-on-rails - 如何从 ActiveRecord 实例中获取关联名称
- math - 函数 |x| 在 0 处的子梯度值是多少?
- angular - 提交按钮在 FormGroup 中不起作用
- python - Tkinter 回调中的异常:AttributeError:“NoneType”对象没有属性“更新”
- reactjs - 这个条件语句在 .map 函数中是如何工作的?
- gradle - 将项目的依赖添加到我的自定义 gradle 插件中
- python - 根据扩展名列表在文件树中查找文件