Maddi
Maddi

Reputation: 27

If statements with undefined variable

I'm not sure what I'm doing wrong I tried to define "if the answer was 'Y' Then premium_membership=true" but I keep getting this error. The beginning is as far as I get when I run the code and the second is my code.

What is your name? John Smith 
How many books are you purchasing today? 5
Are you apart of our membership program? Enter Y for yes or N for No. N
Would you like to join our membership program for $4.95? Enter Y for yes or N for no. N
Thank you for your interest, and thanks for shopping with us!

John Smith 
The customer is purchasing 5.0 books
Traceback (most recent call last):
    File , line 41, in <module>
    if premium_member==True:
    NameError: name 'premium_member' is not defined
>>> 

#ask what customer's name is
customer_name=input("What is your name?")

#ask customer how many books they are purchasing
#define answer as float so you can multiply by price later
books_purchased=float(input("How many books are you purchasing today?"))

#ask customer if they are a member
membership=input("Are you apart of our membership program? Enter Y for yes or N for No.")

#determine if and print if member or not
if membership =='Y':
    premium_member=True
else:
    join=input("Would you like to join our membership program for $4.95? Enter Y for yes or N for no.")

#define join
if join =='Y':
    print("Thank you for joining our Membership Program!")

else:
    print("Thank you for your interest, and thanks for shopping with us!")

#print name
print("\n",customer_name)

#print number of books purchased
print("The customer is purchasing", books_purchased, "books")

#determine if they customer is premium 
if premium_member==True:
    print("Premium Member")
else:
    print("Regular Member")       

#determine if customer gets any free books
if premium_member==True:
    if book_purchased>9:
        print("Congratulations you get two books for free!")
        total_books=book_purchased+2
    else:
        if book_purchased >6 and book_purchased<=9:
            print("Congratulations you get one book for free!")
            total_books=books_purchased+1
        else:
            total_books=books_purchased
else:
    if books_purchased>12:
            print("Congratulations you get two books for free!")
            total_books=book_purchased+2
    else:
        if books_purchased >8 and books_purchased <=12:
            print("Congratulations you get one book for free!")
            total_books=book_purchased+1
        else:
            total_books=books_purchased

Upvotes: 0

Views: 2680

Answers (1)

Curtis Lusmore
Curtis Lusmore

Reputation: 1872

In this section of code

if membership =='Y':
    premium_member=True
else:
    join=input("Would you like to join our membership program for $4.95? Enter Y for yes or N for no.")

you're assigning premium_member=True in the case that membership is equal to 'Y' but in the case that it's not (the else branch) you aren't assigning a value at all, meaning that the variable will be undefined. You should add the line premium_member=False to the else branch.

if membership =='Y':
    premium_member=True
else:
    premium_member=False
    join=input("Would you like to join our membership program for $4.95? Enter Y for yes or N for no.")

Upvotes: 4

Related Questions