#๐Ÿ”’ Why the case statement is not calling my custom function?

25 messages ยท Page 1 of 1 (latest)

jolly niche
#
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()

fierce ventureBOT
#

@jolly niche

Python help channel opened

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.

jolly niche
#

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.

toxic dragon
#

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
finite fox
#

@jolly niche

#

Is there any reasone you are using so may match case statements

jolly niche
jolly niche
finite fox
#

To start with you can use more functions for very case
It'll be readable and won't look messy

jolly niche
wary swallow
finite fox
#
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

jolly niche
finite fox
#

Keep trying
feel free to DM me if you need more help...

jolly niche
finite fox
#

to mimic the break statement

jolly niche
fierce ventureBOT
#
Python help channel closed

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.