0

I am new to python and trying to create a python3 script that uses the Euclidean Algorithm to find the GCD but I keep on getting an error.

Code:

firstnum = input("Enter the first number: ")
secondnum = input("Enter the second number: ")

if firstnum == secondnum:
    print("GCD is: {}").format(firstnum)
    quit()

if firstnum > secondnum:
    while True:
        thirdnum = firstnum % secondnum
        firstnum = secondnum
        secondnum = thirdnum
        if thirdnum == 0:
            print("GCD is: {}").format(firstnum)
            quit()
        else:
            continue

if firstnum < secondnum:
    while True:
        thirdnum = secondnum % firstnum
        secondnum = firstnum
        firstnum = thirdnum
        if thirdnum ==0:
            print("GCD is: {}").format(secondnum)
            quit()
        else:
            continue

Error:

Traceback (most recent call last): File "..\Playground\", line 21, in <module>
   thirdnum = secondnum % firstnum
TypeError: not all arguments converted during string formatting

If there is a way to fix this error please explain how and why it occurred in the first place. I am also new to string formatting so if you know a more efficient way of printing strings with variables please let me know.

1 Answer 1

1

This operation is actually trying to perform string formatting

thirdnum = firstnum % secondnum

If you were attempting to perform a modulus operation you need to convert to int

firstnum = int(input("Enter the first number: "))
secondnum = int(input("Enter the second number: "))
Sign up to request clarification or add additional context in comments.

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.