1

I am learning Python and while working on a simple while loop I get a syntax error but cannot figure out why. Below is my code and the error I get

products = ['Product 1', 'Product 2', 'Product 3']
quote_items = []
quote = input("What services are you interesting in? (Press X to 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 am using NetBeans 8.1 to run these. Below is the error I see after I type in Product 1:

What servese are you interesting in? (Press X to quit)Product 1
Traceback (most recent call last):
File "\\NetBeansProjects\\while_loop.py", line 3, in <module>
quote = input("What services are you interesting in? (Press X to quit)")
File "<string>", line 1
Product 1
SyntaxError: no viable alternative at input '1'
13
  • 3
    Are you running code written for Python 3 on Python 2? Commented Jul 21, 2016 at 18:33
  • I think I am running 3. Is there a way to check? Commented Jul 21, 2016 at 18:34
  • 2
    On a side note, lists do not have any get method: products.get(quote) will raise an error Commented Jul 21, 2016 at 18:36
  • import sys sys.version Commented Jul 21, 2016 at 18:36
  • looks like I am running 2.7.0 Commented Jul 21, 2016 at 18:37

2 Answers 2

4

in Python 3

products = ['Product 1', 'Product 2', 'Product 3']
quote_items = []
quote = input("What services are you interesting in? (Press X to 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 are you interesting in? (Press X to 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 is because lists don't have the attribute '.get()' so you can use

value in list that will return a True or False value

Sign up to request clarification or add additional context in comments.

Comments

1

Use raw_input instead of input. Python evaluates input as pure python code.

quote = raw_input("What services are you interesting in? (Press X to quit)")

3 Comments

Not if he's using Python 3.
@JohnGordon Apparently he's not.
@chepner If he really is using Python 2, then I would have expected a different error from the user input of "Product 1".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.