0
def gcd(a,b):
    if b==0:
       return a
    else:
       return gcd(b,a%b)

When I try to print the output of gcd with any 2 numbers I get the error "TypeError: not all arguments converted during string formatting". Any idea ?

6
  • 2
    Try printing the types of b and a before calling gcd for the first time Commented Jul 24, 2018 at 20:43
  • 2
    Are you taking in user input to pass to this function? Commented Jul 24, 2018 at 20:43
  • 2
    You are not calling the function with integer numbers at all, but with strings. If this is in a program where you are getting the numbers by using the input() function, wrap input's call in a call to int. For example: value = int(input("type the first number: ")) Commented Jul 24, 2018 at 20:43
  • 1
    I think @jsbueno is right about your error; in the future, you should include all of your code (or enough to fully reproduce the problem), including the code that calls this function, where the parameters to this function come from, and where you print the result. Commented Jul 24, 2018 at 20:45
  • Yes I was calling it with strings, thank you very much. Commented Jul 24, 2018 at 20:46

2 Answers 2

0

Make sure you're calling the function with integers. Try casting to int.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to call the function with integers. What you are doing is calling it with strings. If you are using input, cast the string with int().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.