Manuj Singh
Manuj Singh

Reputation: 13

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

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.

Upvotes: 0

Views: 521

Answers (1)

Etoneja
Etoneja

Reputation: 1073

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}"

Upvotes: 4

Related Questions