Python 错误!减去isn';你不支持吗?从输入权重
我试图做一个简单的计算,但一旦用户输入的重量,它不会打印出剩余的重量。带有错误代码 -:“str”和“int”的操作数类型不受支持 这是我的密码。我有什么遗漏吗Python 错误!减去isn';你不支持吗?从输入权重,python,python-3.6,Python,Python 3.6,我试图做一个简单的计算,但一旦用户输入的重量,它不会打印出剩余的重量。带有错误代码 -:“str”和“int”的操作数类型不受支持 这是我的密码。我有什么遗漏吗 def Massallowance(): person = input("Crew or Specialist? ") if person== 'Crew': definedMass = 100 weight = input("Please enter weight: ")
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)
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)
“input”方法将“String”作为输入。因此,每当用户输入一个数字时,它就会直接转换为“字符串”。由于不能用整数对字符串进行子行为,因此会出现错误。这应该是一个简单的修复:
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)
您正在尝试在
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)
input()
返回一个字符串。您需要使用int()
函数将其转换为整数。另外,在print()
中需要str(weight definedMass)
,因为+运算符不能处理字符串和整数