Python Forum

Full Version: Error when trying to square a number
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am completely new to Python and I was following a tutorial on Python 3 using Jupyiter notebook when I got the error bellow ***
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-39-8ab96e18e533> in <module>()
      3 weight_kg = "155"
      4 
----> 5 bmi = weight_kg / (height_m ** 2)
      6 print("bmi: ")
      7 print(bmi)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

I have exactly what is in the video and it worked for him, I can't find the reason it is giving me this error. I have tried clearing the kernal, restarting Jupyiter, trying to isolate the problem like seen below
---------------------------------------
height_m = "2"
bmi = (height_m ** 2)
It comes up with the same error, what is the problem with this line of code? Thanks, Mike.
>>> weight_kg = "155"
>>> type (weight_kg)
<class 'str'>
>>> weight_kg = 155
>>> type (weight_kg)
<class 'int'>
>>>
you cannot multiply strings
Could you check out this video" https://youtu.be/AWek49wXGzI?t=18m5s the link should bring you to the part where he actually seems to multiply strings, but the time is 18:05 if it doesn't work. Does he do it differently than me? I don't see any reason why his would work and mine wouldn't.
Without looking at the video:
You cannot multiply a string. You need to use integers. This is what I show in the previous post
I understand that you cannot multiply strings, but that's why I gave you the link to the video. CS Dojo is the Youtuber who (at 18:05 in the video) shows that he can multiply a string with an integer like shown in the code below.
weight_kg = "50"
height_m = "2"
bmi = weight_kg / (height_m ** 2)
My bad I didn't understand that you were refering to the weight and height, thanks for explaining. That fixed my problem. Sorry again!