Python:TypeError:只能将str(而不是“int”连接到str:存储错误的变量
需要澄清存储为错误值的python变量,代码如下:Python:TypeError:只能将str(而不是“int”连接到str:存储错误的变量,python,scripting,Python,Scripting,需要澄清存储为错误值的python变量,代码如下: userinput1 = int(input('enter start value\n')) userinput2 = int(input('enter stop value\n')) userinput3 = int(input('enter rampup time in seconds\n')) userinput4 = float(input('enter increments delta \n')) userinput5 = inpu
userinput1 = int(input('enter start value\n'))
userinput2 = int(input('enter stop value\n'))
userinput3 = int(input('enter rampup time in seconds\n'))
userinput4 = float(input('enter increments delta \n'))
userinput5 = input('Enter sequence channels: A A A A or D D D D - A Ascend, D Descent , E Exclude \n')
command1 = "RAMP " + str(userinput5) + " " + userinput1 + " " + userinput2 + " " + userinput4 + " " + userinput3
port.write(command1.encode())
#### ERROR #####
command1 = str("RAMP " + str(userinput5) + " " + userinput1 + " " + userinput2 + " " + userinput4 + " " + userinput3)
TypeError: can only concatenate str (not "int") to str
您能给我澄清一下在单变量命令中存储两种类型变量输入的正确方法吗。类型分类已经完成。您只能合并字符串,因此在合并所有用户输入之前,必须将它们“转换”为字符串 例1:
command1 = "RAMP " + " ".join(map(str, [userinput5, userinput1, userinput2, userinput4, userinput3]))
例2:
command1 = f"RAMP {userinput5} {userinput1} {userinput2} {userinput4} {userinput3}"
您只能关联字符串,因此在关联所有用户输入之前,必须将它们“转换”为字符串 例1:
command1 = "RAMP " + " ".join(map(str, [userinput5, userinput1, userinput2, userinput4, userinput3]))
例2:
command1 = f"RAMP {userinput5} {userinput1} {userinput2} {userinput4} {userinput3}"
userinput1
应该是str(userinput1)
,如果要将其连接到字符串中。但是如果使用f-strings,这是不必要的:您需要确定userinput
变量是什么:数字还是字符串。如果你有最新的python,你可以使用格式化字符串,而不是执行str()
:command1=f'RAMP{userinput5}{userinput1}{userinput2}{userinput4}{userinput3}
userinput1
如果你把它连接到字符串,应该是str(userinput1)
,等等。但是如果使用f-strings,这是不必要的:您需要确定userinput
变量是什么:数字还是字符串。如果你有最新的python,你可以使用格式字符串,而不是执行str()
:command1=f'RAMP{userinput5}{userinput1}{userinput2}{userinput4}{userinput3}
非常感谢,真的很有帮助:)非常感谢,真的帮助:)