TypeError: can't multiply sequence to non-int

P

Python

Hello,

I am just learning Python and am going through some examples. I found
one very simple example I can't figure out. Here is the script:

# Enter a number to be squared
x = raw_input("Enter a number: ")
int(x)
sq = x*x
print sq

I am getting the following error:

Enter a number: 5
Traceback (most recent call last):
File "db.py", line 6, in ?
sq = x*x
TypeError: can't multiply sequence to non-int

Why is the function "int(x)" not changing the input to an integer?

Thanks for the help, Jim
 
T

Tyler Eaves

Hello,

I am just learning Python and am going through some examples. I found
one very simple example I can't figure out. Here is the script:

# Enter a number to be squared
x = raw_input("Enter a number: ")
int(x)
sq = x*x
print sq

I am getting the following error:

Enter a number: 5
Traceback (most recent call last):
File "db.py", line 6, in ?
sq = x*x
TypeError: can't multiply sequence to non-int

Why is the function "int(x)" not changing the input to an integer?

It is. The thing is, it doesn't do it "in place".

replace that line with x = int(x)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top