import re
def main_menu():
print('Main Menu')
print('1. String Operation')
print('2. Number Operation')
print('3. Exit')
def emailValid(email):
if re.match(r'[^@]+@[^@]+.[^@]+', email):
return True
return False
def phoneValid(phone):
if re.match(r'01[5-9]\d{8}', phone):
return True
return False
def main():
while (True):
main_menu()
choice = input('\nEnter choice(1, 2 or 3): ')
match choice:
case '1':
print('\nThank you for choosing String operation.')
while True:
rchoice = input('Choice (1-> EmailValidation, 2->PhoneValidation, 3->Exit): ')
match rchoice:
case '1':
while True:
email = input('\nEnter a valid email address: ')
match email:
case True if emailValid(email): # not working
print(f'\nProvided email [{email}] is valid!')
break
case _:
print(f'\nProvided email [{email}] is not valid!')
break
case '2':
break
case '3':
break
case '2':
print('\nThank you for choosing number operation.')
case '3':
print('\nThanks for using switch statements')
break
case _:
print('\nInvalid Input!!!')
main()
#๐ Why the case statement is not calling my custom function?
25 messages ยท Page 1 of 1 (latest)
@jolly niche
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
case True if emailValid(email):
This one is getting skipped by the program and directly jumped in to the next one.
What am I doing wrong here?
@finite fox
It does not give any error messages.
It is just skipping that specific part.
You should use the return value of the check function on the match itself, por example:
email = input('\nEnter a valid email address: ')
match emailValid(email):
case True:
print(f'\nProvided email [{email}] is valid!')
break
case _:
print(f'\nProvided email [{email}] is not valid!')
break
How do I make it work with less match statements?
Thanks a million. It is working now.
To start with you can use more functions for very case
It'll be readable and won't look messy
can you give me some examples please?
I am very new to programming.
Probably use if for when theres only two cases
import re
def main_menu():
print('Main Menu')
print('1. String Operation')
print('2. Number Operation')
print('3. Exit')
def emailValid(email):
if re.match(r'[^@]+@[^@]+.[^@]+', email):
return True
return False
def phoneValid(phone):
if re.match(r'01[5-9]\d{8}', phone):
return True
return False
def emailValidation():
email = input('\nEnter a valid email address: ')
if emailValid(email):
print(f'\nProvided email [{email}] is valid!')
return
else:
print(f'\nProvided email [{email}] is not valid!')
emailValidation() #instead of while
def main():
while (True):
main_menu()
choice = input('\nEnter choice(1, 2 or 3): ')
match choice:
case '1':
print('\nThank you for choosing String operation.')
while True:
rchoice = input('Choice (1-> EmailValidation, 2->PhoneValidation, 3->Exit): ')
match rchoice:
case '1':
emailValidation() # you can break this in more functions
# match emailValid(email):
# case True: # not working
# print(f'\nProvided email [{email}] is valid!')
# break
# case _:
# print(f'\nProvided email [{email}] is not valid!')
# break
case '2':
break
case '3':
break
case '2':
print('\nThank you for choosing number operation.')
case '3':
print('\nThanks for using switch statements')
break
case _:
print('\nInvalid Input!!!')
main()
@jolly niche
check case 1 Email validation
Ahhh man thanks a lot. I wanted to do it in this way. But due to my lack of knowlodge I could not show some courage.
Keep trying
feel free to DM me if you need more help...
why did you use return with the if statement.
to mimic the break statement
sounds complicated. But thank you very kindly.
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.