How do I use the same argument / parameter in two different functions?

1

Example:

 def seno1(x = int(input("Entre com x: ")),n=int(input("Entre com n: "))):
 def cos1(x,n):

How do I take advantage of the value entered by the user of x and n in this function too?

Thank you

    
asked by anonymous 28.05.2017 / 17:33

2 answers

4

Do not try to do all in one line - simply assign variables to your parameters typed with input :

def seno1(x, n):
   ...
def cos1(x,n):
   ...

# e mais abaixo no seu código ponha o trecho:
x = int(input("Entre com x: "))
n = int(input("Entre com n: "))

print(seno1(x, n))
print(cos1(x, n))

In this way you will actually using functions as functions and not only to reorganize the code, but still not being reusable (because the way you did, those input would only execute one time, even if you called the seno1 function several times) - and if you put input and calls within a while or another function, you can have a complete program that remaps multi-valued computations.

    
28.05.2017 / 22:06
1

I think it's best to follow the suggestion of @jsbueno (this is an add-on).

  

Taking advantage of your code, you can use global variables (not indicated):

def seno1(x = int(input("Entre com x: ")),n=int(input("Entre com n: "))):
    global xg # cria variável global para x
    global ng
    xg = x # copia x para a variável
    ng = n
  

To use them, however, you would have to call the function seno1() :

seno1() # chama função
print('variavel global x: ' + str(xg)) // mostra valor de x, fora de seno1()
print('variavel global n: ' + str(ng))
  

A third option is to put the user input into another function:

def inputUsuario():
    x = int(input("Entre com x:"))
    n = int(input("Entre com n:"))
    return(x, n)
  

And then use it in a modified version of sin1 ():

def seno1(x, y): # versão modificada de seno1(), só imprime variáveis
    print('x: ' + str(x) + ', y: ' + str(y))

# utilizando as duas funções

minhaVar = inputUsuario()
seno1(minhaVar[0], minhaVar[1])

Result:

    
29.05.2017 / 04:24