Python while循环语法错误

Python while循环语法错误,python,while-loop,Python,While Loop,我正在学习Python,在进行一个简单的while循环时,我遇到了一个语法错误,但无法解释原因。下面是我的代码和我得到的错误 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_foun

我正在学习Python,在进行一个简单的while循环时,我遇到了一个语法错误,但无法解释原因。下面是我的代码和我得到的错误

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)
我正在使用NetBeans 8.1来运行这些。以下是我键入产品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'
使用原始输入而不是输入。Python将输入计算为纯Python代码

quote = raw_input("What services are you interesting in? (Press X to quit)")
使用原始输入而不是输入。Python将输入计算为纯Python代码

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

在Python 2中

这是因为列表没有“.get”属性,因此可以使用

列表中的值 在Python3中,它将返回真值或假值

在Python 2中

这是因为列表没有“.get”属性,因此可以使用

列表中的值
这将返回一个真值或假值

您是否在Python 2上运行为Python 3编写的代码?我想我正在运行Python 3。有什么方法可以检查吗?在旁注中,列表没有任何get方法:products.getquote将引发错误导入sys.versionlooks,就像我在运行2.7.0一样。你在运行Python 2上为Python 3编写的代码吗?我想我在运行3。有什么方法可以检查吗?在旁注中,列表没有任何get方法:products.getquote将引发错误导入sys.versionlooks,就像我运行的是2.7.0,如果他使用的是Python 3,则不会。@JohnGordon显然不是。@chepner如果他真的使用Python 2,如果他使用的是Python 3,那么我会预期产品1的用户输入会出现不同的错误。@JohnGordon显然不是。@chepner如果他真的使用Python 2,那么我会预期产品1的用户输入会出现不同的错误。
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