【发布时间】:2019-05-05 07:58:35
【问题描述】:
我被困在我试图完成的这段代码上。我希望它打印“没有更多的食谱”,但它会打印两次“让我们选择不同的餐点”。
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")
【问题讨论】:
-
谢谢我其实很喜欢这个解决方案。唯一的这个是一旦它没有找到 my_choice 的任何变体,它就会生成下一个替代配方,即 recipe4。在给出来自 my_choice 的替代食谱之前,我有什么方法可以打印“让我们尝试一些不同的东西”?
-
我喜欢使用 lambda,我必须对其进行更多研究,我会尝试更多地了解它们,因为我觉得它们对未来的开发非常有用。感谢双方提供的两种解决方案,它为我提供了另一种看待事物的方式,并希望更多地学习 python!非常感谢您提供的任何帮助:)
标签: python python-3.x loops for-loop if-statement