How to add elements to the tuple using a function?

Asked

Viewed 1,266 times

1

def funcao():
    int(input('insira um número:'))

tupla = (funcao(), funcao(), funcao(), funcao())

print(tupla.count(9))

print(tupla)

insira um número:9
insira um número:9
insira um número:9
insira um número:9
0
(None, None, None, None)

Process finished with exit code 0
  • 4

    Missing return entry: return int(input('insira um número:'))

  • https://ideone.com/RUdJUB

1 answer

4


You probably don’t understand how the function works. In this case it executes something and discards any information obtained from it. If you want a value obtained within the function to be delivered where the function is called you must use the command return. It has two capacities: it closes the execution flow of the function (it is true that without it also closes at the end of the definition of the code of the function, so it is only mandatory to close if it needs to do before the end, conditionally, of course); and also allows something to result, that is, return a value that you will determine right away, and of course if you need to return a return is mandatory, even to specify what is this value, so only the return.

def funcao():
    return int(input('insira um número:'))
tupla = (funcao(), funcao(), funcao(), funcao())
print(tupla.count(9))
print(tupla)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Could also have done without the function:

tupla = (int(input('insira um número:')), int(input('insira um número:')), int(input('insira um número:')), int(input('insira um número:')))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.