Positional path error in Python3

Asked

Viewed 31 times

-2

I’m doing a test code for python, and I’m getting the following error:

Traceback (Most recent call last): File "test.py", line 7, in g= test('Type a number: ') Typeerror: test() Missing 2 required positional Arguments: 'y' and 'z'

My way of solving this problem is only for study purposes. Basically what I want the script to do is show the questions of the variables g, h, i and save the results in a, b, c respectively. Then I want my function to return the stored values. If you can help me without altering the code structure, just so that I let it work properly.

def teste(x, y, z):
    a= str(input(x))
    b= str(input(y))
    c= str(input(z))
    return a, b, c

#Programa principal
g= teste('Digite um numero:  ')
h= teste('Digite uma frase:  ')
i= teste('Digite um texto:  ')

teste(x=g, y=h, z=i)
print(f'{a} {b} {c}')
  • 2

    Without altering the structure has no way, it is wrong and makes no sense. You made the call of function 4x, and in the first three passed only one parameter while are expected 3, so the error. You could explain line by line, like a table test what your code is doing?

  • Anderson, I don’t know the test table, but I found it interesting, so I’m gonna study it and put my code on top of it to see where I’m going wrong. By the way, I understood what you meant by calling 4x the function, thanks!! When I said that I did not want to change the structure of the program, I referred to not adding lines or syntaxes that I do not know, of course I want to make it functional and it will be necessary to change something in this regard

1 answer

1


Apparently what you want is to define a function that reads from the user three values and returns them in the variables a, b and c. If you will read the user values it makes no sense to pass parameters to the function.

def le_valores():
  a = input('Valor 1: ')
  b = input('Valor 2: ')
  c = input('Valor 3: ')

  return a, b, c

The return of the function will be a tuple with the three values read. From it you can use the multiple assignment to set values outside the function:

g, h, i = le_valores()

print(f'{g}, {h}, {i}')

Thus displaying the values read by the function.

  • Thank you Anderson, this is what I wanted to do, thanks for the tip!

Browser other questions tagged

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