Thujhan Yogeswaran
Thujhan Yogeswaran

Reputation: 1

Python coding error, error is TypeError: unsupported operand type(s) for +: 'int' and 'str'

I am consistently getting this error message in python 3.4.2,

TypeError: unsupported operand type(s) for +: 'int' and 'str'. 

(I must use this version), this is the accused line of the error:

User_input_string = int(input("What is the answer to", (Random_num + Operator + Random_num2)))

Upvotes: 0

Views: 73

Answers (2)

jramirez
jramirez

Reputation: 8685

I'm guessing those vars are not strings

(str(Random_num) + str(Operator) + str(Random_num2))

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121266

You are trying to add up integers and a string:

Random_num + Operator + Random_num2

You'd normally have to convert to a common type first, like a string:

str(Random_num) + Operator + str(Random_num2)

but you can also use string formatting:

User_input_string = int(input("What is the answer to {} {} {}".format(
    Random_num, Operator, Random_num2))

The latter gives you more flexibility in how the output string is formed.

Upvotes: 3

Related Questions