how to resolve Missing 2 required positional argument python

Asked

Viewed 1,993 times

1

I’m new to Python and programming and I’m trying to create a calculator (interactive way I think kkkk) using the content I’m learning to create functions, but it’s giving this error on the last line (where I am trying to call the functions, because when I put to run without this line only processed the program successfully without showing anything). Follow the error that appears and my code:

(numero(calculus(message)) Typeerror: calculo() Missing 2 required positional argument: 'num2'

def numero(num1,num2):
    num1 = float(input("Digite um numero: "))
    num2 = float(input("Digite outro numero: "))
def calculo(result,num1,num2):
    calculo = int(input("Digite um o respectivo numero de acordo com o calculo que deseja realizar \n 1-)adição \n 2-)subtração \n 3-)divisão \n 4-)potenciação \n 5-)multiplicação "))
    if calculo == 1:
        result = num1 + num2
    elif calculo == 2:
        result = num1 - num2
    elif calculo == 3:
        result = num1 / num2
    elif calculo == 4:
        result = num1 ** num2
    elif calculo == 4:
        result = num1 * num2

def mensagem(result):
    print(" o resultado é",result)
(numero(calculo(mensagem)))

In fact it gave this error with the number too, but I have no idea how to solve, I know it is a simple problem but I am in difficulty. Can you help me?

1 answer

1

Win your code presents some indentation flaws, I do not know if it was failure to pass the question but let’s go.

Failure indicates that your method calculo is not receiving two parameters and it needs three the failure occurs in this call (numero(calculo(mensagem))).

Your methods should return the values, otherwise they will only be available in the method. Then editing your methods:

# removi os parematros, não são necessários você que irá informá-los
# dentro do método
def numero():
    num1 = float(input("Digite um numero: "))
    num2 = float(input("Digite outro numero: "))
    return num1, num2

# removi o parametro result, já que ele será calculado pelo metodo
# e aqui ele será retornado
def calculo(num1,num2):
    calculo = int(input("Digite um o respectivo numero de acordo com o calculo que deseja realizar \n 1-)adição \n 2-)subtração \n 3-)divisão \n 4-)potenciação \n 5-)multiplicação "))
    if calculo == 1:
        result = num1 + num2
    elif calculo == 2:
        result = num1 - num2
    elif calculo == 3:
        result = num1 / num2
    elif calculo == 4:
        result = num1 ** num2
    elif calculo == 4:
        result = num1 * num2

    return result

def mensagem(result):
    print(" o resultado é",result)

# aqui a função numero retorna uma tupla, porque está retornando 2
# valores, então vamos receber primeiro a tupla e passar depois para
# as funções
resposta = numero()
mensagem(calculo(resposta[0], resposta[1]))

When a method returns more than one variable it returns as default in tuple format without having to put the () in Return

  • So I made the changes that you said, when I changed the indentation (in "if" and "Elif" which was what I think you were referring to) it was wrong, maybe I got it wrong too. And I added the Return at the end but the error persists, now speaking that only one argument is missing, it is like this: message(calculus(number)) Typeerror: calculus() Missing 1 required positional argument: 'num2'.

  • @Victóriajulia modified your code take a look at it in my answer

Browser other questions tagged

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