Python Forum
A basic question from a noob
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A basic question from a noob
#1
Smile 
Hi, everyone!
It is my first time here, sorry for the stupid questions.
Wrote a code for the Euclid’s algorithm, used to find the greatest common divisor of two integers greater than zero, which seems to work but not quite sure how to break the loop if the two integers are the same. Help much appreciated.
# 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)
while n != m:
    if n > m:
        n = int(n - m)
    else:
        m = int(m - n)
print "The GCD is:", n
Reply
#2
(Mar-13-2021, 12:48 PM)AncientMantra Wrote: not quite sure how to break the loop if the two integers are the same
not sure what you ask. The loop will run till m == n. That is what the condition while n != m: does.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
And if you start with m == n the loop doesn't yun at all. What do you think the program is doing wrong?
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  "Not callable" (probably a very basic question, task 1 in a course) kwiide 2 2,066 Sep-03-2020, 06:17 PM
Last Post: kwiide
  Noob question about import files dxfrelince 1 1,867 Mar-15-2019, 01:34 PM
Last Post: ichabod801
  I need help on a basic coding question Presssure 1 4,183 May-23-2018, 05:12 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020