0

I'm trying to get the message "INVALID VALUE" when the user doesn't enter the correct characters, but the program is restarted without printing the message. Can you help me?

# Print "Male" when the user types "M" and "Female" when the user types "F"

def m_ou_f():
    mens_erro = "INVALID VALUE"
    while True:
        try:
            sex = str(input("Type M for Male or F for Female: "))
            sex == "M" or sex == "F" or sex == "f" or sex == "m"
        except:
            print(mens_erro)
            continue
        else:
            return sex
            break

while True:
    sex = m_ou_f()
    try:
        sex == "M" or sex == "F" or sex == "f" or sex == "m"
    except:
        print("INVALID VALUE!")
        continue
    else:
        if sex == 'M' or sex == 'm':
            print("Male")
            break
        elif sex == 'F' or sex == 'f':
            print("Female")
            break
Lucas Tadeu
  • 91
  • 1
  • 1
  • 4
  • Are you familiar with the basics of try/except? As an aside, be careful about using `except:` like this, see https://stackoverflow.com/questions/54948548/what-is-wrong-with-using-a-bare-except. – AMC Mar 09 '20 at 23:56

2 Answers2

0

There is no need to write the same thing two times.

The try statement works as follows. [From Python Docs]

  • First, the try clause (the statement(s) between the try and except keywords) is executed.

  • If no exception occurs, the except clause is skipped and execution of the try statement is finished.

  • If an exception occurs during the execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the except keyword, the except clause is executed, and then execution continues after the try statement.

  • If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with a message as shown above.

In your case, try block was executed successfully, that's why it does not get into except block.

This will print the INVALID VALUE!.

while True:
    try:
      sex = str(input("Type M for Male or F for Female: "))
    except ValueError:
      print("INVALID VALUE!")
    if sex == 'M' or sex == 'm':
        print("Male")
        break
    elif sex == 'F' or sex == 'f':
        print("Female")
        break
    else:
      print("INVALID VALUE!")

Let me know if I have to add more explanation.

ParthS007
  • 2,581
  • 1
  • 22
  • 37
0
sex == "M" or sex == "F" or sex == "f" or sex == "m"

This line will not throw an exception -- it will just evaluate to 'false' if the wrong character is typed.

Perhaps you want something like:

if sex == "M" or sex == "F" or sex == "f" or sex == "m" : raise Exception(mens_erro) 

As for the second part -- your function won't return an exception, so no need to put it into a try catch:

while True:
    sex = m_ou_f()
    if sex == 'M' or sex == 'm':
        print("Male")
        break
    elif sex == 'F' or sex == 'f':
        print("Female")
        break

But if you did:

try:
  sex = m_ou_f();
except:
  continue
Gerard ONeill
  • 3,914
  • 39
  • 25