Trying to figure out NameError: in Python program

Trying to figure out NameError: in python program. The program is supposed to give a user three choices for different types of carpet then prompts them for the height and width. Based on the price of the carpet and what they input for their height and width it should print out the total cost for them. I am getting a NameError saying carpetgrade is not defined. Could anyone check it out and explain why it isnt?

def full_cost(height, width, carpetgrade):

    cost = carpetgrade * area

    area = height * width

    return cost
        
polyester = 45

wool = 75

acrylic = 100

print("Choose a grade of carpet. There is polyester, wool, and acryilic")

carpet_selection = (input("Which carpet grade do you choose?"))

if (carpet_selection == polyester):
    carpetgrade = 45
                       
elif (carpet_selection == wool):
    carpetgrade = 75
    
elif (carpet_selection == acrylic):
    carpetgrade = 100

height = int(input("What is the height of the room?"))

width = int(input("What is the width of the room?"))

complete_cost = full_cost(height, width, carpetgrade)

print("The cost of your carpet is ,complete_cost")

Here is the error: enter image description here

Traceback (most recent call last):
  File "C:UsersAustinDesktopAssignment 3.py", line 34, in <module>
    complete_cost = full_cost(height, width, carpetgrade)
NameError: name 'carpetgrade' is not defined

Answer

In your if statements, you are checking if the input carpet_selection is equal to polyester, wool, or acrylic. But, I’m assuming you want to check if the user is inputting these actual words (from your image – you entered the word “wool”.) You’ll need to put these in quotes, because right now, your code is looking to see if the user input is equal to your previously defined variables, which have names polyester, wool, and acrylic. To check for the input being equal to a string, you need to put these words in quotes, since that’s the syntax for Python strings.

Since the input will always initially be a string, and your code is checking to see if the input is equal to those variables (which are of type int), none of the conditions will ever evaluate to be true, and thus, carpetgrade will never be defined. So, your if statements should look something like this:

if (carpet_selection == "polyester"): carpetgrade = 45

elif (carpet_selection == "wool"): carpetgrade = 75

elif (carpet_selection == "acrylic"): carpetgrade = 100

Now, there is one more caveat – in the function full_cost, you are doing arithmetic with the variable carpetgrade. So, before you pass the variable into the function, you’ll need to do something to convert carpetgrade into a value that you can work with in arithmetic/mathematical operations – you could try setting up some more if conditions to check for what carpetgrade could be equal to, and then set it to the integer/numerical value that you want.

Leave a Reply

Your email address will not be published. Required fields are marked *