Instead of telling you the answer, let me tell you how I debug your code.
1. First I copied your code into my text editor and ran it as it is.
So the first thing I fixed was the indentation error by adding one space. and then I ran the code. The program ran, and ask for a number ( indicating line3 is running) and then asked for another number but after that, I got an error.
Error:
File "C:\Users\XXX\Documents\ATOM\ex10", line 17
print "The GCD is:", n
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("The GCD is:", n)?
Process returned 1 (0x1) execution time : 0.175 s
Press any key to continue . . .
an error was in line 17. your print function did not include ()
Think of a function as.... a box that can take input (or multiple inputs)
sometimes input can be omitted, (function will run without any variable carried over)
but for now, Assume this Input >>>> goes into Function >>>> Output.
so all the functions, always have a special place to take the input.
function ( *space to take the input*)
so if you are writing a function, () must be followed by the function name. print is a function, that takes input and displays it on the screen. so the input goes inside the box.
#Combine first_name and last_name.
Combine_Name ( first_name, last_name)
#this function will run, but there is nothing to print. This will print empty line
print()
#this will print numbers
print (34234234234)
#this will print text AASDBWDWD
print ( "AASDBWDWD")
So after fixing the issue on = line 17 ( Missing parenthesis )
I was able to run the code. here is the output.
Output:
Type in a number greater than 0:300
Type in a number greater than 0:150
The GCD is: 150
Process returned 0 (0x0) execution time : 10.254 s
Press any key to continue . . .
One thing I want to stress is writing your instruction in Peudocode first, and then write the function. then solve each part.
# Euclid's greatest common divisor algorithm
#Greatest Common Divisor Calculator
# Take in two numeric integer values. find a greatest common divisor
# get input from a user.
# Calculate GCD based on Euclid's algorithm
# Display GCD value to the user
After that, you can fill in the individual lines.
# Euclid's greatest common divisor algorithm
#Greatest Common Divisor Calculator
# Take in two numeric integer values. find a greatest common divisor
# get input from a user.
number = input("Type in a number greater than 0:")
n = int(number)
number = input("Type in a number greater than 0:")
m = int(number)
# Calculate GCD based on Euclid's algorithm
while n != m:
if n > m:
n = int(n - m)
else:
m = int(m - n)
# Display GCD value to the user
print "The GCD is:", n
This is also helpful, when you debug.. by turning some part of the code into a command. so you can see where it is messing up.
# Euclid's greatest common divisor algorithm
#Greatest Common Divisor Calculator
# Take in two numeric integer values. find a greatest common divisor
# get input from a user.
number = input("Type in a number greater than 0:")
n = int(number)
number = input("Type in a number greater than 0:")
m = int(number)
# Calculate GCD based on Euclid's algorithm
# while n != m:
# n = int(n - m)
# else:
# m = int(m - n)
#
# Display GCD value to the user
print ("The GCD is:", n)
IF your code does not run as expected... I would like to partition it and test the sections. and keep going. the terminal will tell you a bit of information about the error. Google every time you encounter an error and it will help you debug faster. I intentionally did not fix the code here, but please fix the errors in your lines and have fun running it.
Always write pseudo-code comments for readability!
build the first part first and test.
# Euclid's greatest common divisor algorithm
number = input("Type in a number greater than 0:")
n = int(number)
number = input("Type in a number greater than 0:")
m = int(number)
#You can insert the line here just so you can check the values for debugging
print ( "the value of n and M",n "the value of m", m)
if you pass it.. add second part and add check point there.
# Euclid's greatest common divisor algorithm
number1 = input("Type in a number greater than 0:")
n = int(number1)
number2 = input("Type in a number greater than 0:")
m = int(number2)
#You can insert the line here just so you can check the values for debugging
print ( "\nUser Input\n" "n=",n, "m=", m)
while n != m:
if n > m:
n = int(n - m)
else:
m = int(m - n)
#You can insert the line here just so you can check the values for debugging
print ( "\nAfter a while loop\n" "n=",n, "m=", m)
# Display GCD value to the user