3

Good fine morning compatriots, I'm having some issues with a personal project. I'm writing a text-based murder mystery where the player reads different accounts and must guess who is the murderer. My issue arises with repeating navigation options. Here is a snippet of my current solution:

fbjn = input()
def testimonies(sub):
    if sub == "Harb":
        print_out(harbt)
        print_out("\nWhose testimony would you like to hear? ")
        fbjn = input()
        testimonies(fbjn)

I'm wondering if there's an easier/less redundant way of coming back around to the beginning of a function and being able to repeat it until one wants to move on. Thanks in advance

SOLUTION: Figured out loops.

def testimonies():
    while True:
        sub = input()
        if sub = "Harb":
            print(harbt)
            continue

Hardest thing was understanding scope regarding loops

Thanks for the help!

  • 3
    Do the answers to this [question](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) help at all? – quamrana Jul 15 '22 at 16:59
  • 6
    You definitely don't want to use recursion this way -- if the user does it too many times your game will crash because there's only room for so many function calls on the stack. – Charles Duffy Jul 15 '22 at 17:01
  • 2
    In general, you should think about structuring your game as a state machine. – Charles Duffy Jul 15 '22 at 17:01
  • The quick answer here is, instead of recursively calling a function which will overload the stack, use a while loop. At the top of the loop, get the user input, then take action on that input. Then it loops once again following the same logic. – JNevill Jul 15 '22 at 17:09
  • @quamrana very helpful to a point. Because the player is supposed to declare multiple unique inputs (suspect names to get suspect testimonies), a while loop, as described in the linked post, is only partially applicable as I run into an infinite loop issue. However I suspect this is a skill issue on my part. The linked post is incredibly illuminating however and incredibly helpful. Thanks – Blake Cameron Jul 15 '22 at 18:12

0 Answers0