New user needs help
Wayne Folta
wfolta at netmail.to
Sat Feb 21 06:47:27 CET 2004
> def getnum1(a):
> a = input("What is the first number?")
The 'a' in the def statement is a placeholder which exists only within
your function and is initially assigned the value of the first argument
to your function. References to 'a' within the function refer to this
'a', not the 'a' in your main program, which you set to 0 and which
remains 0. That kind of info is found under the title of a variable's
"scope".
If you want to return a value from your function, you would have
something in your main program like:
a = getnum()
b = getnum()
where you defined getnum something like:
def getnum():
a = int(input ("Enter a number > 0: "))
while (a <= 0):
a = int (input ("Enter a number that is greater than 0: "))
return a
Of course, if I enter "foo" instead of a number, it dies because it
can't convert "foo" into an integer. You could also handle that case.
I'd also remark that it strikes me as a little strange to have your
getnum function recurse if you don't get a non-zero number. In this
case workable since I guess a person wouldn't enter "0" repeatedly
forever, but in other cases you could overflow the stack
Is this Euclid's extended algorithm? It hurts my head.
> def euclid(num1, num2, num3, num4):
> if num1 < num2:
> num3, num4 = num1, num2
> num1, num2 = num4, num3
> euclid(num1, num2, num3, num4)
> elif num2 != 0:
> num3, num4 = num1, num2
> num1 = num4
> num2 = num3 % num4
> euclid(num1, num2, num3, num4)
> else:
> print "The lowest common factor is: ", num1
> a = 0
> getnum1(a)
> b = 0
> getnum2(b)
> x, y = 2, 100
> euclid(a, b, x, y)
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list