正确结束循环

时间:2018-12-03 23:46:27

标签: python python-3.x loops for-loop if-statement

我被困在试图完成的代码上。我希望它打印“没有剩余的食谱”,但是它打印两次“让我们选择另一餐”。

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":
        print("Lets find you a different meal") # this prints out twice when the alternate recipes run out.
    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()

        if len(my_choice) in food_bank.items() > len(v):
            print("Sorry we have no more recipes")

2 个答案:

答案 0 :(得分:0)

首先,if choice == "y"elif choice == "n"语句应放在if my_choice in v:

第二,当elif choice == "n"时,您需要知道这是否是最后的配方(即'recipe4')。

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()

答案 1 :(得分:0)

如果您重复输入'n',则循环返回到'recipe4',其中不包含'my_choice'/'[“ a”,“ b”,“ c”],则不会返回新值为“选择”设置。因此它会从上次看到“ 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")