Other menu options needed for my Py code

**I have received a feedback about my Login Authentication code.
I cant think of any other options to satisfy this feedback, aside from ending my code using else statement for any other characters that user may type in. Brains Trust, suggestions please!

Feedback below:

“Make it so the user must select an option in the menu so they can’t input something that is not in the menu that way you can do away with the following lines -
If user inputs any character (other than 1,2,3 or 4)
*Else user inputs any other characters in menu (other than 1,2,3 and secret code)”

https://replit.com/@QBrd1/ProjectLoginWithHash#main.py

while True:
  print("\n\033[32m", "Welcome - Please login to access your account", "\033[0m", sep="")
  menu=input("\nPress 1 - New User \nPress 2 - Existing User\nPress 3 - Delete Account\n\U0001F449 ")
  if menu=="1":
    NewUser() #new user registration
    login() #sign in as current user
    break
  elif menu == "2":
    login() #sign in as current user
    break
  elif menu =="3":
    deleteAccount()
  elif menu == "777":
    keys = db.keys() #returns all key names in db
    for key in keys:
      print(f"{key}:{db[key]}") #prints key name and key value
  else:
    print("Please choose only between the selections above: ")
    continue

This is correct. In python, a language without switch/case, the way to do it is either if/elif/else.
there are some other way but overall if/elif/else is the most pythonic (as in preferred by python users)

2 Likes

Everything is correct
As @whileTRUEpass said that this is the best way if/elif/else without using switches and cases

1 Like

Python actually does has match and case. I was very surprised when I learned that!

2 Likes

they added it to the latest python? Such a bad idea … and the behaviours is not the same as this is pattern matching. So for this example is good, but in other cases the order of things might cause havoc in a match/case.
Still great @MiloCat , i missed this change in 3.10

2 Likes

Yup there are many more obscure features in Python
I agree though that in this case (lol) using match isn’t a good idea.

Thanks everyone who responded! I appreciate them all!

1 Like

I learned something today, which makes this thread one of my all time fabs now! Gotta play with match/case (maybe even in my sill;y console tetris game) hope reply is 3.10+

1 Like

I’ve checked the link you shared, and on google. Looks to me that its a lot like similar to the simple If, elif, else statement, especially its the output. It still allows user to enter a value that I didnt define. I’m a noob in Py, so please point out what im missing here.

1 Like

Yup, you probably shouldn’t use it, it’s only useful when you have a ton of elifs instead
I just put the link to tell @whileTRUEpass :smiley:

1 Like

Makes sense. Code is a lot cleaner. Thanks for pointing it out, and the shared link btw. Cheers!

Well … I will use it anyhow. I always likes switch/case anyhow and while i say you are right and nobody should do what i am about to do. I will still do it :slight_smile: