i learning python , while working on simple while loop syntax error cannot figure out why. below code , error get
products = ['product 1', 'product 2', 'product 3'] quote_items = [] quote = input("what services interesting in? (press x quit)") while (quote.upper() != 'x'): product_found = products.get(quote) if product_found: quote_items.append(quote) else: print("no such product") quote = input("anything else?") print(quote_items)
i using netbeans 8.1 run these. below error see after type in product 1:
what servese interesting in? (press x quit)product 1 traceback (most recent call last): file "\\netbeansprojects\\while_loop.py", line 3, in <module> quote = input("what services interesting in? (press x quit)") file "<string>", line 1 product 1 syntaxerror: no viable alternative @ input '1'
in python 3
products = ['product 1', 'product 2', 'product 3'] quote_items = [] quote = input("what services interesting in? (press x quit)") while (quote.upper() != 'x'): product_found = quote in products if product_found: quote_items.append(quote) else: print("no such product") quote = input("anything else?") print(quote_items)
in python 2
products = ['product 1', 'product 2', 'product 3'] quote_items = [] quote = raw_input("what services interesting in? (press x quit)") while (quote.upper() != 'x'): product_found = quote in products if product_found: quote_items.append(quote) else: print "no such product" quote = raw_input("anything else?") print quote_items
this because lists don't have attribute '.get()' can use
value in list
return true
or false
value
Comments
Post a Comment