-1

I´m new to Stackoverflow and to Python. As my first own program I wrote a text game where you enter (a) or (b) and decide this way which decision your character makes. Its working out quite well, but I have one problem. If the user enters, for example, "a" on the first decision, "b" on the second decision, but something invalid on the third, the next valid input will trigger the first decision again instead of the third. I tried to make a short version which portrays my problem. Any help is appreciated.

def test():
    while True:
        input_zero = input("1. > ")
        if input_zero == "a":
            print("a")
            input_a = input("2. > ")
            if input_a == "a":
                print("a, a")
                break
            elif input_a == "b":
                print("a, b")
                break
            else:
                print("Invalid input.")
        elif input_zero == "b":
            print("b")
            input_b = input("2. > ")
            if input_b == "a":
                print("b, a")
                break
            elif input_b == "b":
                print("b, b")
                break
            else:
                print("Invalid input.")
        else:
            print("Invalid input.")


test()
muern1
  • 13
  • 4
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Random Davis Oct 07 '20 at 17:06
  • Also, `I have the feeling that the code is incredibly laboriously written` yes, it is, so it's much _more_ laborious to try to read. Perhaps post a [mre] instead. – Random Davis Oct 07 '20 at 17:07
  • Will do, ill try to recreate the problem! – muern1 Oct 07 '20 at 17:14

1 Answers1

0

So, packaging your choices into a dictionary, similar to that shown below, should make it slightly easier to manage the choices here, I think (there's almost certainly a better way than this). Then add to the empty string each time a choice is made and try to access the dictionary. If the choice is in the dictionary then it will recover a text string and an end-state, which will enable us to end the game when we need to.

This approach also makes testing easier by using itertools to generate all possible combinations of states so you can work out which are missing. If an end_state is found (a value of 1 in the second position of the tuple), then you get the game over message and it closes the loop. If the element isn't in the dictionary, then the last selection was removed and the invalid_input function is called.

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()
    
Andrew
  • 1,072
  • 1
  • 7
  • 15
  • Hey, thank you so much for taking time to write this. I really appreciate it. This approach makes the code so much easier to look at and it also works way better! Now I just have to make this an application or something and I can ask my friends to play it. Big thanks!! – muern1 Oct 07 '20 at 18:46