Python Forum
Error when trying to square a number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error when trying to square a number
#1
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.
Reply
#2
>>> weight_kg = "155"
>>> type (weight_kg)
<class 'str'>
>>> weight_kg = 155
>>> type (weight_kg)
<class 'int'>
>>>
you cannot multiply strings
Reply
#3
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.
Reply
#4
Without looking at the video:
You cannot multiply a string. You need to use integers. This is what I show in the previous post
Reply
#5
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)
Reply
#6
My bad I didn't understand that you were refering to the weight and height, thanks for explaining. That fixed my problem. Sorry again!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Im at square one even with trying to install python origen 1 322 Jan-12-2024, 05:39 AM
Last Post: ndc85430
  Even number code syntax error MrCeez 1 2,247 May-02-2021, 06:43 PM
Last Post: Larz60+
  square root of 5 input numbers mrityunjoy 1 1,995 Jun-10-2020, 11:08 AM
Last Post: perfringo
  square root of 6 input numbers mrityunjoy 3 2,557 Jun-07-2020, 06:35 AM
Last Post: ndc85430
  Excpetion Handling Getting Error Number gw1500se 4 2,327 May-29-2020, 03:07 PM
Last Post: gw1500se
  Error when entering letter/character instead of number/integer helplessnoobb 2 6,983 Jun-22-2019, 07:15 AM
Last Post: ThomasL
  How to simplify square finding program? meknowsnothing 3 2,837 Jun-11-2019, 08:20 PM
Last Post: meknowsnothing
  cropping a picture (always square) Leon 1 2,104 Aug-13-2018, 10:04 AM
Last Post: Leon
  Square and Cube roots. jarrod0987 2 5,343 Apr-13-2018, 09:30 PM
Last Post: casevh
  Understanding square bracket use wahsape 4 3,886 Jan-26-2018, 01:17 PM
Last Post: kismat77

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020