我不确定我做错了什么我试图定义"如果答案是'Y'
那么premium_membership=true
"但我一直收到这个错误。当我运行代码时,开头就是我得到的,第二个是我的代码。
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
答案 0 :(得分:4)
在这段代码中
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.")
您在premium_member=True
等于membership
的情况下分配'Y'
但在不是else
分支的情况下分配premium_member=False
)你根本没有赋值,这意味着变量将是未定义的。您应该将行else
添加到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.")
分支。
MessageBox