在我的标题中,我的意思是:
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'中的产品,那么该程序将返回到第一行再次写入产品。
答案 0 :(得分: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, "'")
答案 1 :(得分: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 '