0

I'm trying to create functions that will calculate the areas of different shapes. This is what I have so far, but on the lines that print the results I always get the external error "Cannot read property 'toFixed' of null" and I do not know what that means or how to fix it

import math

def displayWelcome():
  print "Welcome to my area and perimeter calculator"

def calcAreaCircle(radius):
  (3.14159) * (radius ** 2)

def calcPerimeterCircle(radius):
  2 * 3.14159 * radius

def calcAreaSquare(side):
  side ** 2

def calcPerimeterSquare(side):
  side * 4

def calcAreaRect(width, height):
  width * height

def calcPerimeterRect(width, height):
  (2 * width) + (2 * height)

def calcAreaTriangle(base, height):
  (0.5 * base * height)

displayWelcome()
radius = 3.56
area = calcAreaCircle(radius)
perimeter = calcPerimeterCircle(radius)
print('Circle   : area = {0:.2f}, perimeter = {1:.2f}' .format(area, perimeter))

side = 9.23
area = calcAreaSquare(side)
perimeter = calcPerimeterSquare(side)

width = 2.9
height = 14.22
area = calcAreaRect(width, height)
perimeter = calcPerimeterRect(width, height)

base = 7.97
height = 5.31
area = calcAreaTriangle(base, height)
  • What do you mean by "external error"? How exactly are you running the program, and what is the *exact* error message you get (copy and paste it, or take a screenshot if necessary)? – Karl Knechtel Mar 24 '20 at 16:03
  • 2
    None of your methods computing a value is returning it, so each return one return None – azro Mar 24 '20 at 16:04
  • I have `TypeError: unsupported format string passed to NoneType.__format__` when i try your code, and this logic regarding my previous comment – azro Mar 24 '20 at 16:05
  • The error I get (when I run it in trinket.io) says "ExternalError: TypeError: Cannot read property 'toFixed' of null on line 31 in main.py" – Austin Mangelson Mar 24 '20 at 16:09
  • 1
    https://stackoverflow.com/questions/7129285/what-is-the-purpose-of-the-return-statement –  Mar 24 '20 at 16:10

1 Answers1

1

According to @azro advice I make it work by adding return to your functions, and changing the 'your string'.format() print.

And in your displayWelcome() function you used the python 2.7 way to print in console. Next time be aware that python 2.7 will be deprecated soon.

import math

def displayWelcome():
  print("Welcome to my area and perimeter calculator")

def calcAreaCircle(radius):
  return (3.14159) * (radius ** 2)

def calcPerimeterCircle(radius):
  return 2 * 3.14159 * radius

def calcAreaSquare(side):
  return side ** 2

def calcPerimeterSquare(side):
  return side * 4

def calcAreaRect(width, height):
  return width * height

def calcPerimeterRect(width, height):
  return (2 * width) + (2 * height)

def calcAreaTriangle(base, height):
  return (0.5 * base * height)

displayWelcome()
radius = 3.56
area = calcAreaCircle(radius)
perimeter = calcPerimeterCircle(radius)
print('Circle   : area = {}, perimeter = {}' .format(area, perimeter))

side = 9.23
area = calcAreaSquare(side)
perimeter = calcPerimeterSquare(side)

width = 2.9
height = 14.22
area = calcAreaRect(width, height)
perimeter = calcPerimeterRect(width, height)

base = 7.97
height = 5.31
area = calcAreaTriangle(base, height)
Zenocode
  • 656
  • 7
  • 19