-1

I am trying to build a class feature where my player selects a class for a choose your own adventure game I am building. My code looks as such:

    def class_choice(choice1, choice2, choice3, choice4):
        choice = input("What class would you like to select? Note that you must type the class \ exactly as displayed: ")
        if choice == choice1:
            print("You have selected the Engineer class!")
            def engineer_start(choice1, choice2):
                input("test")
        if choice == choice2:
            print("You have selected the G.I. class!")
            input("test")
        if choice == choice3:
            print("You have selected the F.S.S. class!")
            input("test")
        if choice == choice4:
            print("You have selected the Journalist class!")
            input("test")
        if choice != class_choice('Engineer', 'G.I.', 'F.S.S.', 'Journalist'):
            print()
            print(f"{choice} is not a valid input!")
            return class_choice(choice1, choice2, choice3, choice4)


    class_choice('Engineer', 'G.I.', 'F.S.S.', 'Journalist')

My issue is that when a player does not put in a correct input, it does return to the initial function (class_choice), but it does not execute 'if choice != class_choice'.

I tried writing out:

if choice != class_choices(choice1, choice2, choice3, choice4):
   print()
   print(f"{choice} is not a valid input!")
   return return class_choice(choice1, choice2, choice3, choice4)

My hope was that by changing the choices to be changed later when I define my arguments, it would check if the argument was one of my choices, and if not, return to them the original function. However, it feels like it completely skips over that line of code!

Lord Death
  • 11
  • 2
  • Your function is never returning anything, so will return `None` when it reaches an exit point. Thus, when you say `if choice != class_choice(...)` you're really saying `if choice != None`. This is probably not what you're looking for. – Nathaniel Ford Feb 02 '23 at 00:48
  • Consider wrapping your `if` statement into an infinite [`while True` loop](https://www.digitalocean.com/community/tutorials/how-to-construct-while-loops-in-python-3). – Jens Feb 02 '23 at 00:49
  • Aha, thank you! That is what I was looking for. Thank you for the help Nathaniel! – Lord Death Feb 02 '23 at 00:50
  • I don't understand why the choices are parameters of the function, but the code in the `if` statements require that each choice will be a particular value. What's supposed to happen if you call it with arguments in a different order, like `class_choice('G.I.', 'F.S.S.', 'Journalist', 'Engineer')`? – Barmar Feb 02 '23 at 00:50

2 Answers2

0

I can see that you're using the function class_choice in the condition of the same function, that leads to an infinite loop!

Try:

def class_choice(choice1, choice2, choice3, choice4):
    choice = input("What class would you like to select? Note that you must type the class \ exactly as displayed: ")
    if choice == choice1:
        print("You have selected the Engineer class!")
        def engineer_start(choice1, choice2):
            input("test")
    elif choice == choice2:
        print("You have selected the G.I. class!")
        input("test")
    elif choice == choice3:
        print("You have selected the F.S.S. class!")
        input("test")
    elif choice == choice4:
        print("You have selected the Journalist class!")
        input("test")
    else:
        print()
        print(f"{choice} is not a valid input!")
        return class_choice(choice1, choice2, choice3, choice4)

class_choice('Engineer', 'G.I.', 'F.S.S.', 'Journalist')
Nova
  • 406
  • 2
  • 13
-1

You're looking for a function like this:

def select_class(options: List[str]) -> str:
    choice = input("What class?")
    if choice in options:
        print(f"You have selected {choice}.")
        return choice
    else
        print(f"'{choice}' is not a valid class.")
        return select_class(options)

choice = select_class(['Engineer', 'GI', 'FSS', 'Journalist'])
print(f"'{choice}' has been selected.")

Here, you pass in a list of options (List[str]), and you ask the user to input their choice. If that choice is in the list (the first if clause) you return that choice. If it is invalid, you re-call the select class function.

Not that your formulation of this:

if choice != select_class(...)

Is not what you're looking for. What happens with that formulation is it first calls select_class() and then compares it to choice. There are two issues with this: first, your function never returns anything, so it thus will always eventually return None, which won't match the input. Secondly, what you really want is to call that function only if the choice isn't valid.

As noted by Barmar, using recursion (a function calling itself) for this in Python is not strictly advised due to a maximum recursion depth. The other way to write this is:

def select_class(options):
    choice = input("What class?")
    while choice not in options:
        print(f"{choice} is not valid.")
        choice = input("What class?")
    print(f"You have chosen {choice}")
    return choice
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102