-4

在我的标题中,我的意思是:

    myCellar = ["doritos", "chips", "chocolates", ""]
    productsInDemand = input("Write a product : ")

   for supply in myCellar :
      if productsInDemand == supply:
         print("This product we have : '",productsInDemand ,"'")
         break
   else:
      print("This product we have not : '",productsInDemand ,"'")
      (go back to the line 1)

如果我要编写一个在“mycellar”中不存在的产品,那么程序将返回第一行再次编写产品。

4

2 回答 2

3

只需使用无限while True循环:

while True:
    myCellar = ["doritos", "chips", "chocolates", ""]
    productsInDemand = input("Write a product : ")
    if productsInDemand in myCellar:
        print("This product we have : '", productsInDemand, "'")
        break
    print("This product we have not : '", productsInDemand, "'")
于 2012-11-05T12:31:51.060 回答
1

尝试这样的事情:

myCellar = ["doritos", "chips", "chocolates", ""]
productsInDemand = input("Write a product : ")

while productsInDemand not in myCellar :
    print("This product we have not : '",productsInDemand ,"'")
    productsInDemand = input("Write a product : ")

print("This product we have : '",productsInDemand ,"'")

输出:

Write a product : foo
This product we have not : ' foo '
Write a product : bar
This product we have not : ' bar '
Write a product : doritos
This product we have : ' doritos '
于 2012-11-05T12:36:11.450 回答