繁体   English   中英

Python while循环语法错误

[英]Python while loop Syntax Error

我正在学习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 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)

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

这是因为列表没有属性'.get()',所以您可以使用

value in list中的value in listvalue in list将返回TrueFalse

使用raw_input代替input Python将input评估为纯python代码。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM