I am a beginning programmer writing in Python 3.5 for my Computer Concepts III class. This week we are working on data validation using try/except blocks and Boolean flags, modifying a program we made last week. I've almost completed my weekly assignment except for one thing. I can't figure out why I'm getting stuck in a while loop. This is the loop in question:
while not valid_data:
cont = input("Would you like to order another? (y/n) ")
if cont.lower not in yorn:
valid_data = False
else:
valid_data = True
yorn is ["y", "n"]
Here is the whole program for context:
# Program Lesson 6 Order
# Programmer Wiley J
# Date 2016.02.13
# Purpose The purpose of this program is to take an order for cookies.
# Import Class/Format Currency
import locale
locale.setlocale(locale.LC_ALL, '')
# Define Variables
boxes = 0
cost = 3.50
qty = 0
items = 0
yorn = ["y", "n"]
# Banner
print("Welcome to The Cookie Portal")
# Input
valid_data = False
while not valid_data:
name = input("Please enter your name: ")
if len(name) > 20:
print()
print("Not a valid name")
valid_data = False
elif len(name) == 0:
print("You need to enter a name")
valid_data = False
else:
print("Hello", name)
valid_data = True
cont = input("Would you like to place an order? (y/n) ")
# Process
while cont.lower() not in yorn:
cont = input("Would you like to place an order? (y/n) ")
while cont.lower() == "y":
valid_data = False
while not valid_data:
print("Please choose a flavor:")
print("1. Savannahs")
print("2. Thin Mints")
print("3. Tagalongs")
try:
flavor = int(input("> "))
if flavor in range (1, 4):
items += 1
valid_data = True
else:
valid_data = False
except Exception as detail:
print("Error", detail)
valid_data = False
while not valid_data:
try:
boxes = int(input("How many boxes? (1-10) "))
if boxes not in range (1, 11):
print("Please choose a number between 1 and 10")
valid_data = False
else:
qty += boxes
valid_data = True
except Exception as detail:
print("Error", detail)
print()
print("Please enter a number")
valid_data = False
while not valid_data:
cont = input("Would you like to order another? (y/n) ")
if cont.lower not in yorn:
valid_data = False
else:
valid_data = True
# Output
if cont.lower() == "n":
cost *= qty
print()
print("Order for", name)
print("-------------------")
print("Total Items = {}".format(items))
print("Total Boxes = {}".format(qty))
print("Total Cost = {}".format(locale.currency(cost)))
print()
print("Thank you for your order.")
I wouldn't be surprised if there are other issues with this code but they are most likely by design as per the requirements of the assignment. Any additional feedback is welcome.
It seems you are missing function-paranthesis in the end of "lower", like so:
if cont.lower() not in yorn:
Your problem is here:
if cont.lower not in yorn:
lower is a method, it should be:
if cont.lower() not in yorn:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With