2012-11-05 33 views
-3

在我的標題我的意思是:Python;回到一個行,如果條件=假

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」 excist的產品後,程序將回到第一行再寫一個產品。

回答

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

我會把'myCellar = 「多力多滋」 ,「芯片」,「巧克力」,「」]'在while循環之前。 – glglgl

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 ' 
相關問題