2

I am completely new to Python.

I have a simple Program that finds the greatest common denominator (GCD) of two numbers. It goes like this

def gcd(a, b):
 if a == b:
     return a
 else:
     if a > b:
         return gcd(a-b, b)
     else:
         return gcd(a, b-a)

This text is currently in a notepad document titled gcd.py on my desktop. As you can see the program doesn't actually print anything it just returns the greatest common denominator.

I need to actually print the results by doing the following print(gcd(25,10)). I don't know where I can put that line to get the results I need. I tried inputting it into the windows command line by doing python print(gcd(25,10)). But that doesn't work. Neither does print(gcd(25,10)) in the python interpreter.

I think I am supposed to put it in the python interpreter, but that the interpreter has to be set to the correct directory of my desktop (where the gcd file is found)but I cant seem to do that. I tried o.chdir and when I print(cwd) it prints the correct directory but it still doesn't work.

This is my first time using python so I'm a little confused.

ifconfig
  • 6,242
  • 7
  • 41
  • 65
user7959439
  • 153
  • 1
  • 9
  • Are you trying to have it take input from the terminal and then print out the answer? Please clarify what you want to do. – ifconfig Sep 12 '17 at 01:29
  • 1
    I think you need to take a little look-see at the [docs](https://docs.python.org/3/)... – cs95 Sep 12 '17 at 01:29
  • There's a reasonably good tutorial at Python.org. You should take the time to work your way through it. This isn't a tutorial site. – Ken White Sep 12 '17 at 01:38

2 Answers2

1

Try this. You need to print it out in the same file or import it from another:

def gcd(a, b):

 if a == b:

     return a

 else:

     if a > b:

         return gcd(a-b, b)

     else:

         return gcd(a, b-a)

print(gcd(25,10))
whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44
1

If you want to print from inside the file, put this line at the end after the function definition as suggested in the previous answer.

If you want to print from outside the file, navigate to the directory and type:

from gcd import gcd

This will load your function into the interpreter. You can then print the value.