Euclid's Algorithm in Python?

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Sun Aug 7 17:20:23 EDT 2005


Erik the Red wrote:
> So, I did the following:
> ---
> a=input("Give me an integer")
> b=input("Give me another integer")
> 
> def gcd(a,b):
> 
>     if a < b:
>          a, b = b, a
>     while b != 0:
>          a, b = b, a % b
>     return a
> ---
> But, in the xterm, it terminates after "Give me another integer."
> 
> Is Euclid's Algorithm supposed to be implemented in such a way as to be
> used as a tool to find the GCD of two integers, or have I
> misinterpreted the intent of the algorithm?

You do not do anything after both input() calls. You define the function, but
never call it.

Add
print gcd(a, b)
to the end and it will print your result.

Note that the variable names a and b in the function don't have anything to
do with your two input variables.

Reinhold



More information about the Python-list mailing list