慕的地10843
因此,我认为,将您的选择打包到字典中,类似于下面所示的那样,应该会更容易管理这里的选择(几乎肯定有比这更好的方法)。然后每次做出选择时添加到空字符串并尝试访问字典。如果选择在字典中,那么它将恢复一个文本字符串和一个结束状态,这将使我们能够在需要时结束游戏。这种方法还可以生成所有可能的状态组合,从而使测试变得更加容易itertools,以便您可以找出缺少的状态。如果找到 end_state(元组第二个位置的值为 1),那么您将收到游戏结束消息并关闭循环。如果该元素不在字典中,则删除最后一个选择并invalid_input调用该函数。def test(): choice_dict = {"a": (dP_lvl1.path_a, 0), "b": (dP_lvl1.path_b, 0), "c": (dP_lvl1.path_c, 1) "bb": (dP_lvl2.path_bb, 0), "aa": (dP_lvl2.path_aa, 0), "ba": (dP_lvl2.path_ba, 0), "ab": (dP_lvl2.path_ab, 0), "aaa": (dP_lvl3.path_aaa, 0), "aab": (dP_lvl3.path_aab 0), "aba": (dP_lvl3.path_aba, 0), "abb": (dP_lvl3.path_abb, 0), "bab": (dP_lvl3.path_bab, 0), "bba": (dP_lvl3.path_bba} 0), "bbb": (dP_lvl3.path_bbb, 0), "aaaa": (dP_lvl4.path_aaaa, 0), "abaa": (dP_lvl4.path_abaa, 0), "aaba": (dP_lvl4.path_aaba, 0), "aaab": (dP_lvl4.path_aaab, 1), "bbba": (dP_lvl4.path_bbba, 0), "bbab": (dP_lvl4.path_bbab, 0), "babb": (dP_lvl4.path_babb, 0), "abbb": (dP_lvl4.path_abbb, 0), "abba": (dP_lvl4.path_abba, 1), "abab": (dP_lvl4.path_abab, 0), "aabb": (dP_lvl4.path_aabb, 0), "baab": (dP_lvl4.path_baab, 0), "bbaa": (dP_lvl4.path_bbaa, 1), "baba": (dP_lvl4.path_baba, 0), "baaa": (dP_lvl4.path_baaa, 0), "bbbb": (dP_lvl4.path_bbbb, 0),} # etc. you get the idea decisions = "" playing = True while playing: decision = input("choose an option 'a' or 'b':") decisions += decision try: data, end_state = choice_dict[decisions] print(data) if end_state: playing = False print("Game over") else: continue except KeyError: decisions = decisions[:-1] invalid_input()