Python: TypeError: can only concatenate str (not "int") to str : variable stored wrong
Python: TypeError: can only concatenate str (not "int") to str : variable stored wrong
您好需要澄清 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 = 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}"
您好需要澄清 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 = 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}"