Python Function Won't Be Called


#1

I’m trying to create functions for each shape to be called if the shape is selected but the function won’t be called and it returns the error NameError: name ‘a’ is not defined

from math import pi

def circle_area():
    radius = float (input("Enter radius: "))
    area = pi * radius ** 2
    print("Area: %.2f." % (area))

option = input("Enter what you want calculated. P for Preimiter V for Volume or A for Area: ")
option.upper()
       
if option == "A" or option == "a":
    
    shape = input("Circle = C, Ellipse(Oval) = E ")

    if shape == "C" or shape == "c": 
        circle_area()

#2

what python version are you using? in python2, input() is evaluated as code, if you are using python2, use raw_input


#3

Input has worked in previous versions only when I started using functions did this error occur


#4

still, can you answer my question? Can you also provide a copy of the working code (before implementing function then?)


#5

I’m using python 3.6.2
as for the code before using functions here is a small part of it

from math import pi

option = input("Enter what you want calculated. P for Preimiter V for Volume or A for Area: ")
option.upper()

if option == "A" or option == "a":
    
    shape = input("Circle = C ")

    if shape == "C" or shape == "c": 
        radius = float (input("Enter radius: "))
        area = pi * radius ** 2
        print("Area: %.2f." % (area))

#6

i ran your code here:

it runs fine?


#7

I remember it working fine the last times I tried but yeah I guess I need to use raw_input instead


#8

raw_input() doesn’t exist anymore in python3, the only way i can replicate your error (or get close, can’t be certain) is by using input in python2 entering a


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.