-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 に答える