1

Hello so today I've decided to start learning Python. I'm studying c++ in school and programming in c++ for about 1 year, so I thought it'll be a great idea to start writing base algorithms from c++ to python. I want to write the the greatest common divisor in python but it gives me this error:

File "d:\proiecte\c++ to python algorithms\cmmdc.py", line 12, in <module>
    print(cmmdc(a,b))
  File "d:\proiecte\c++ to python algorithms\cmmdc.py", line 7, in cmmdc
    return cmmdc(b, a % b)
TypeError: not all arguments converted during string formatting

here is the code:

print ("alorithm to solve gcd from c++ to python! ")

def cmmdc(a, b):
    if b == 0:
        return a
    else:
        return cmmdc(b, a % b)
print ("write the first number: ")
a = input()
print ("write the second number: ")
b = input()
print(cmmdc(a,b))
2
  • 1
    you probably want to cast your input to int. int(input()) should work Commented Feb 22, 2021 at 17:24
  • yepp it worked thank u very much :) Commented Feb 22, 2021 at 17:27

2 Answers 2

2

input() gives a string. You need to use a = int(input()) and b = int(input()).

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

Comments

2

what you have done is, you have taken the input of a and b as string type but you are treating them as an integer. So you need to be typecast the string while taking input. Refer to the code below:

print ("alorithm to solve gcd from c++ to python! ")

def cmmdc(a, b):
    if b == 0:
        return a
    else:
        return cmmdc(b, a % b)
print ("write the first number: ")
a = int(input())    #type casting the string input into integer.
print ("write the second number: ")
b = int(input())
print(cmmdc(a,b))

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.