Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 错误!减去isn';你不支持吗?从输入权重_Python_Python 3.6 - Fatal编程技术网

Python 错误!减去isn';你不支持吗?从输入权重

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: ")

我试图做一个简单的计算,但一旦用户输入的重量,它不会打印出剩余的重量。带有错误代码

-:“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)
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)
,因为+运算符不能处理字符串和整数