Python 正确结束循环

Python 正确结束循环,python,python-3.x,loops,for-loop,if-statement,Python,Python 3.x,Loops,For Loop,If Statement,我被困在我试图完成的代码上。我希望它打印“没有更多的食谱了”,但它打印“让我们选择不同的膳食”两次 首先,if choice==“y”和elif choice==“n”语句应该在if my_choice in v: 其次,当elif choice==“n”时,您需要知道这是否是最后一个配方(即“recipe4”) 如果重复输入'n',则一旦循环到达'recipe4',该循环不包含'my_choice'/'[“a”,“b”,“c”],则不会为'choice'设置新值。所以它看到上次的“n”,第二次

我被困在我试图完成的代码上。我希望它打印“没有更多的食谱了”,但它打印“让我们选择不同的膳食”两次


首先,
if choice==“y”
elif choice==“n”
语句应该在
if my_choice in v:

其次,当
elif choice==“n”
时,您需要知道这是否是最后一个配方(即“recipe4”)


如果重复输入'n',则一旦循环到达'recipe4',该循环不包含'my_choice'/'[“a”,“b”,“c”],则不会为'choice'设置新值。所以它看到上次的“n”,第二次打印不同的用餐文本

以下是我对您的代码的建议

my_choice = ["a","b","c"]
bank = {"recipe1":[["a","b","c"], "d", "e", "f"],
        "recipe2":[["a","b","c"], "g", "h", "i"],
        "recipe3":[["a","b","c"], "j", "k", "l"],
        "recipe4":["x", "y", "z"]}

# Create a list from your dictionary
bankList = list(bank.items())
# And then sort it according to whether 'my_choice' is in a given recipe
# This way, the later loop will go through the matching recipes first
bankList.sort(key=lambda recipe: my_choice in recipe[1], reverse=True)

choice = None
for k, v in bank.items():
    if choice == "n":
        print("Let's find you a different meal")
    print(f"The meal we have chosen for you is {k,v}")
    print("Do you like your meal? y/n")
    choice = input()

    # If the user did not enter 'y' or 'n', keep asking until they do
    while choice not in ["y", "n"]:
        print("Please select y or n to keep your meal or select a different recipe.")
        print(f"The meal we have chosen for you is {k,v}")
        print("Do you like your meal? y/n")
        choice = input()

    if choice == "y":
        print("Enjoy your meal!")
        break
else:
    # This code will only run if the loop completes without breaking
    # In other words, if all recipes are considered and the user never says 'y' to any of them
    print("Sorry, we have no more recipes")

看起来我需要知道配方的每个位置,以及在“bank”对象中哪个配方在哪个位置之后。理想情况下,“银行”将容纳100或1000个配方,因此基本上我需要将下一个配方放在列表中,这与我的选择不匹配-这将告诉代码停止..正确吗?谢谢你的帮助:)@SinclairAkoto正确。所以最好使用while循环。谢谢,我非常喜欢这个解决方案。唯一的问题是,一旦我的选择没有任何变化,它就会产生下一个替代配方,即recipe4。在我选择的替代配方给出之前,我有没有办法打印“让我们尝试一些不同的东西?”我喜欢lambda的使用,我必须对它进行更多的研究,我会尝试更多地理解它们,因为我觉得它们对未来的发展非常有用。感谢双方提供的这两种解决方案,它为我提供了另一种看待事物的方式,我想进一步学习python!非常感谢您提供的任何帮助:)
my_choice = ["a","b","c"]
bank = {"recipe1":[["a","b","c"], "d", "e", "f"],
        "recipe2":[["a","b","c"], "g", "h", "i"],
        "recipe3":[["a","b","c"], "j", "k", "l"],
        "recipe4":["x", "y", "z"]}

for k,v in bank.items():
    if my_choice in v:
        print(f"Your meal we have chosen for you is {k,v}")
        print("Do you like your meal? y/n")
        choice = input()
        if choice == "y":
            print("Enjoy your meal!")
            break
        elif choice == "n":
            if "recipe4" != "recipe4":
                print("Lets find you a different meal") # this prints out twice when the alternate recipes run out.
            else:
                print("Sorry we have no more recipes")
        else:
            print("Please select y or n to keep your meal or select a different recipe.")
            print(f"Your meal we have chosen for you is {k,v}")
            print("Do you like your meal? y/n")
            choice = input()
my_choice = ["a","b","c"]
bank = {"recipe1":[["a","b","c"], "d", "e", "f"],
        "recipe2":[["a","b","c"], "g", "h", "i"],
        "recipe3":[["a","b","c"], "j", "k", "l"],
        "recipe4":["x", "y", "z"]}

# Create a list from your dictionary
bankList = list(bank.items())
# And then sort it according to whether 'my_choice' is in a given recipe
# This way, the later loop will go through the matching recipes first
bankList.sort(key=lambda recipe: my_choice in recipe[1], reverse=True)

choice = None
for k, v in bank.items():
    if choice == "n":
        print("Let's find you a different meal")
    print(f"The meal we have chosen for you is {k,v}")
    print("Do you like your meal? y/n")
    choice = input()

    # If the user did not enter 'y' or 'n', keep asking until they do
    while choice not in ["y", "n"]:
        print("Please select y or n to keep your meal or select a different recipe.")
        print(f"The meal we have chosen for you is {k,v}")
        print("Do you like your meal? y/n")
        choice = input()

    if choice == "y":
        print("Enjoy your meal!")
        break
else:
    # This code will only run if the loop completes without breaking
    # In other words, if all recipes are considered and the user never says 'y' to any of them
    print("Sorry, we have no more recipes")