0

I am currently writing a short program for my intro to computer science course, and my code is returning "none" even though I am pretty sure my definitions are clear. Don't mind the bulky naming for my functions and stuff, its a course requirement. The objective of the code is to be able to choose a shape and then directly input the required information without a written prompt, and then the program will RETURN the area of the chosen shape. I have been breaking my teeth over this for the past few hours, playing with it, but my code is returning none regardless of what I do. Any advice? Please refrain from blatantly giving me new code because I can get in trouble for that, just perhaps point me in the direction of my problem.

import math

# the following functions are built to calculate each shape

def circle_area(rad):
    return math.pi*rad**2

def rectangle_area(side_one, side_two):
    return side_one*side_two

def triangle_area(edge):
    return (math.sqrt(3)/4)*(edge**2)

# the following function as assigned using the above functions

def shape_area():

    shape_choice = input("Choose shape (1=circle, 2=rectangle, 3=triangle):")

    if shape_choice == 1 or 3:
        length_one = input("")

    elif shape_choice == 2:
        length_two, length_three = input("")

    if shape_choice == 1:
        circle_area(length_one)

    elif shape_choice == 2:
        rectangle_area(length_two, length_three)

    elif shape_choice == 3:
        triangle_area(length_one)

    elif shape_choice != 1 or 2 or 3:
        return None

I'm not sure why all my code isn't going into the specific code gray box, but I hope my code is more or less clear.

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • 2
    Your shape_area() function will always return None. Most of the routes through it fall out of the bottom and return None by default. – quamrana Apr 18 '20 at 11:11
  • I am not familiar with python, but you only have a return statement and that is the return None – Serban Gorcea Apr 18 '20 at 11:13
  • Does this answer your question? [Python Function Returning None](https://stackoverflow.com/questions/21471876/python-function-returning-none) – dspencer Apr 18 '20 at 11:17
  • From [documentation](https://docs.python.org/3/library/functions.html#input) input function returns string. Would [How to compare string and integer in python?](https://stackoverflow.com/questions/17661829/how-to-compare-string-and-integer-in-python) thread be useful? –  Apr 18 '20 at 11:21

2 Answers2

5

You are not returning the area values, just calculating them.

    if shape_choice == 1:
        return circle_area(length_one)

    elif shape_choice == 2:
        return rectangle_area(length_two, length_three)

    elif shape_choice == 3:
        return triangle_area(length_one)

Also, as @NomadMonad also mentions, the statement:

if shape_choice == 1 or 3:

is always true as 3 is a thing. Instead use if shape_choice == 1 or shape_choice == 3:

Your final elif can be an else as it is the final condition that can be returned. You could even remove it as python will return None if nothing is returned anyway.

GTBebbo
  • 1,198
  • 1
  • 8
  • 17
  • Thank you. I thought that because the function itself dictates a return that it would work without returning the actual function – Nurit Eliana Apr 18 '20 at 11:15
  • 1
    Another option is: if shape_choice in [1, 3]: – quamrana Apr 18 '20 at 11:16
  • You can imagine a function as having a value, the value is whatever it returns. A good way of thinking about it is, if I copy the code from the function instead of calling the function, and then remove the return statement, that is what you get. Some values / variables for you to use. – GTBebbo Apr 18 '20 at 11:18
  • `if shape_choice in {1,3};` – Patrick Artner Apr 18 '20 at 11:41
1

This line

if shape_choice == 1 or 3

Always evaluates to True

You should change it to

if shape_choice == 1 or shape_choice == 3

Similarly

elif shape_choice != 1 or 2 or 3
NomadMonad
  • 651
  • 6
  • 12