Python Forum
NameError: name 'pi' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name 'pi' is not defined
#1
I'm working on a Python program to calculate the area of different shapes (square, rectangle, and circle). I've written the code for squares and rectangles, but I'm stuck on the circle part. Here's what I have so far:
def calculate_area(shape, side1, side2=None):
  """Calculates the area of a square or rectangle."""
  if shape == "square":
    return side1 * side1
  elif shape == "rectangle":
    return side1 * side2
  else:
    print("Invalid shape. Please enter 'square' or 'rectangle'.")
    return None

# Example usage for square and rectangle
area_square = calculate_area("square", 5)
area_rectangle = calculate_area("rectangle", 3, 4)

print("Area of square:", area_square)
print("Area of rectangle:", area_rectangle)

# Now trying to add circle functionality
def calculate_circle_area(radius):
  """Calculates the area of a circle."""
  # Here's where I'm stuck!
  # I know the formula for circle area is pi * radius^2,
  # but I'm getting an error "NameError: name 'pi' is not defined".
  return radius * radius  # This line causes the error

# Trying to call the circle function
# circle_area = calculate_circle_area(7)  # This line would also cause an error
# print("Area of circle:", circle_area)
The code throws a NameError: name 'pi' is not defined error when trying to calculate the circle area.
Can you help me fix the code so it calculates the circle area correctly using the mathematical constant pi (approximately 3.14159)?
Wordle Unlimited
Reply
#2
Hope it helps

def calc(shape, arg1, arg2=None):
    pi = 3.14
    if shape == 'square':
        return f'Square: {arg1*arg1}'

    elif shape == 'rectangle':
        return f'Rectangle: {(2*arg1) + (2*arg2)}'

    elif shape == 'circle':
        return f'Circle: {pi * arg1**2}'

    else:
        return 'Error! Wrong shape'


print(calc('square', 8))
print(calc('rectangle', 5,3))
print(calc('circle', 4))
output
Output:
Square: 64 Rectangle: 16 Circle: 50.24
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
math is a good module for calculating things!

import math

print( math.pi )
Or numpy:

from numpy import pi

print(pi)
I think both modules just define pi to a reasonable number of decimal places.

But if you use the module gmpy2 you can get any number of decimal places and finally read the secret message that God coded into the number pi!

Let me know when you have it decoded please!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 1,624 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,624 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 3,998 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 4,427 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,937 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 2,502 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 14,830 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 17,402 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 4,061 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 3,326 Jul-26-2021, 04:36 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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