Python:TypeError:只能将str(而不是“int”)连接到str:variable存储错误

2024-06-16 10:12:33 发布

您现在位置:Python中文网/ 问答频道 /正文

需要澄清存储为错误值的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

您能给我澄清一下在单变量命令中存储两种类型变量输入的正确方法吗。类型种姓已经完成


Tags: 代码类型inputvalue错误intenterstr
1条回答
网友
1楼 · 发布于 2024-06-16 10:12:33

您只能关联字符串,因此在关联所有用户输入之前,必须将它们“转换”为字符串

例1:

command1 = "RAMP " + " ".join(map(str, [userinput5, userinput1, userinput2, userinput4, userinput3]))

例2:

command1 = f"RAMP {userinput5} {userinput1} {userinput2} {userinput4} {userinput3}"

相关问题 更多 >