我试图做一个简单的计算,但是一旦用户输入了重量,它就不会打印出剩余的剩余重量。附带错误代码
--'str'和'int'的不受支持的操作数类型
这是我的代码。有什么我想念的吗?
def Massallowance():
person = input("Crew or Specialist? ")
if person== 'Crew':
definedMass = 100
weight = input("Please enter weight: ")
print("Allowance" + weight-definedMass)
elif person== 'Specialist':
definedMass = 150
weight = input("Please enter weight: ")
print("Allowance" + weight-definedMass)
答案 0 :(得分:0)
input()
为您提供了字符串,您无法从数字中减去字符串-这没有任何意义。您应该使用int
或float
def Massallowance():
person = input("Crew or Specialist? ")
if person== 'Crew':
definedMass = 100
weight = int(input("Please enter weight: "))
print("Allowance" + weight-definedMass)
elif person== 'Specialist':
definedMass = 150
weight = int(input("Please enter weight: "))
print("Allowance" + weight-definedMass)
答案 1 :(得分:0)
“输入”方法将“字符串”作为输入。因此,无论何时用户输入数字,它都会直接转换为“字符串”。由于无法用整数替换字符串,因此会出现错误。这应该是一个简单的解决方法:
def Massallowance():
person = input("Crew or Specialist? ")
if person== 'Crew':
definedMass = 100
weight = int(input("Please enter weight: "))
print("Allowance" + weight-definedMass)
elif person== 'Specialist':
definedMass = 150
weight = int(input("Please enter weight: "))
print("Allowance" + weight-definedMass)
答案 2 :(得分:0)
您正在尝试在minus
和str
之间进行int
操作
首先将输入字符串转换为int
,然后执行操作。
weight = int(input("Please enter weight: "))
更新后的代码就像
def Massallowance():
person = input("Crew or Specialist? ")
if person== 'Crew':
definedMass = 100
weight = int(input("Please enter weight: "))
print("Allowance" + weight-definedMass)
elif person== 'Specialist':
definedMass = 150
weight = int(input("Please enter weight: "))
print("Allowance" + weight-definedMass)