Warm tip: This article is reproduced from serverfault.com, please click

Python: TypeError: can only concatenate str (not "int") to str : variable stored wrong

发布于 2020-12-01 17:26:13

Hi Need clarification for python variable stored as wrong value , here is code :

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

Can you please clarify me correct method to store both type variable input in single variable command. type caste was done already.

Questioner
Manuj Singh
Viewed
0
Etoneja 2020-12-02 01:30:10

You can concate only strings, so before concate all your userinputs you must "convert" them into strings

Example1:

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

Example2:

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