Using the def function

2

I would like to create a function def for the first input and then return it on the third line.

Example:

def se():
    sex = input('Digite seu sexo:')

while sex != 'M' and sex != 'F':
    #não sei como se retorna esta parte: sex = input('Digite seu sexo:')
    
asked by anonymous 21.11.2017 / 18:48

1 answer

2

def is a language for constructing the language, it is not a function, it is used to declare and define a function.

Your code, because of the posted indentation, does not do what you want, Python is sensitive to indentation. Then

def se():
    sex = input('Digite seu sexo:')

is the function e

while sex != 'M' and sex != 'F':

It's a loose code.

What you probably want is this:

def getSex():
    while True:
        sex = input('Digite seu sexo:')
        if sex == 'M' or sex == 'F':
            return sex

print(getSex())

See working on ideone . Also I put GitHub for future reference.

The return closes a function and it can optionally deliver a result to the caller, as every mathematical function does (programming does not invent anything, it's all math, almost everything taught in school). >

Of course there is a lot to improve on this, but the focus of the question is this.

    
21.11.2017 / 18:53