Checking if a number of options are true


#1

I am trying to check if user input is equal to a number of options. I have never done this before. I had a look online and decided to use the any[] function. My code for what I am trying to do is below.

commands = str(input(“Please choose either to PLAY or read the RULES\n”))
while str(commands) == any[“PLAY”, “Play”, “play”]:
break

I also then have an if and else statement.

if str(commands) == any[“RULES”, “Rules”, “rules”, “RULE”, “Rule”, “rule”]:
print(rules which I have written out)
else:
print(“Sorry that command wasn’t recognised. Please try again.”)
print(commands)
break

My main issue is trying to check for multiple user inputs, but I am not sure if I have structured the while loop well. Full loop below.

commands = str(input(“Please choose either to PLAY or read the RULES\n”))
while str(commands) == any[“PLAY”, “Play”, “play”]:
break
if str(commands) == any[“RULES”, “Rules”, “rules”, “RULE”, “Rule”, “rule”]:
print(""“These are the rules to the game etc.”"")
enter = input(“Press enter to continue\nEnter…”)
break
else:
print(“Sorry that command wasn’t recognised. Please try again.”)
print(commands)
break

CodeAcademy doesn’t seem to add indentation. I have actually indented :joy:


#2

any() is a function call, so why do you use square brackets?


#3

The truth is you don’t want to get in the business of relying on a user for text input. Think about the number of ways you could misspell/capitalize/mutate San Francisco. A limitless amount.
It’s always going to be hard to get the user to input the exact string you wanted.

Instead give them a menu that only lets them select a set of options (1-10, A-E etc etc)
Making that menu involves a bit of programming concepts you’ve seen
do/while loops nested if’s(switch in other languages)

When I first started, I tried implementing HUGE regexes to ‘solve’ the problem of user typos when typing out one choice of many cities.
What a silly waste of time(I did learn how to regex pretty well!) When I could have displayed each city corresponding to a number and had them pick 1-20.


#4

why not? If you want to add GUI functionality later, its a possible to already go for text input. (or a dropdown + database)

Yes, it adds complexity in the validation.

But its really depending on the specifications of the application. Who knows what the future of this application will be

but i agree, if its just a simple practice applications the number approach is better.


#5

The domain of codecademy is beginners learning syntax.
Why talk about GUI/databases when the issue is getting a console app game menu to work lol.

That’s the level of complexity we should be concerned with here - not Algorithmic complexity.


#6

regex wouldn’t be massively difficult if you want to validate the word rule(s).

True, but sometimes you new learners make a mistake on purpose (like how difficult validating user input is), to learn from it. You can’t protect new learners from every mistake down the road

anyway, we could could argue about this all day, because there is no definitive right or wrong answer. As much as i would enjoy that, we do go a bit off-topic then.


#7

Thanks for the replies guys. I realised the issue with the original code but the idea of offering a menu seems like a good way to go, I’ll have a look into it. There is no requirement for a GUI (it’s only for a high school CS project). I have been having some other issues with other parts of the code, I’ll put up a post about them soon. I have the week off to get it finished.