444,035 Members | 1,384 Online
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

# New user needs help

 P: n/a I am new to using Python. Everytime I run this program it prints "The lowest common factor is 0", no matter what numbers I use. Can anybody see anything wrong with my program? Thanks in advance. def getnum1(a): a = input("What is the first number?") if a == 0: getnum1(a) def getnum2(b): b = input("What is the second number?") if b == 0: getnum2(b) 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) Jul 18 '05 #1
4 Replies

 P: n/a John wrote: I am new to using Python. Everytime I run this program it prints "The lowest common factor is 0", no matter what numbers I use. Can anybody see anything wrong with my program? Thanks in advance. def getnum1(a): a = input("What is the first number?") if a == 0: getnum1(a) def getnum2(b): b = input("What is the second number?") if b == 0: getnum2(b) 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) This is a nice simple example of what is called "scope" and how it can trick a beginner. Put as simply as possible,"a" and "b" are actually defined twice. Once "locally" in the getnum functions and another time "globally" . When you set a and b to the user input in the functions the other a and b have no idea about this assignment. Below is a slight revision which return the a and b set by the user back to where they are called. By putting a simple print statement in the script you can see where the values are set and that can help you. I hope that helps. There is probbaly a couple of other changes you could make as well but nothing too major, in my opinion. def getnum1(a): a = input("What is the first number?") if a == 0: getnum1(a) return a def getnum2(b): b = input("What is the second number?") if b == 0: getnum2(b) return b def euclid(num1, num2, num3, num4): print 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 b = 0 a=getnum1(a) b=getnum2(b) x, y = 2, 100 euclid(a, b, x, y) Jul 18 '05 #2

 P: n/a Thank you very much. This solved the problem. I now see where the problem was. This was driving me bonkers! I'm sure I will post here frequently so thanks in advance for everyone's help. "omission9" wrote in message news:64******************@nwrddc03.gnilink.net... John wrote: I am new to using Python. Everytime I run this program it prints "The lowest common factor is 0", no matter what numbers I use. Can anybody see anything wrong with my program? Thanks in advance. def getnum1(a): a = input("What is the first number?") if a == 0: getnum1(a) def getnum2(b): b = input("What is the second number?") if b == 0: getnum2(b) 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) This is a nice simple example of what is called "scope" and how it can trick a beginner. Put as simply as possible,"a" and "b" are actually defined twice. Once "locally" in the getnum functions and another time "globally" . When you set a and b to the user input in the functions the other a and b have no idea about this assignment. Below is a slight revision which return the a and b set by the user back to where they are called. By putting a simple print statement in the script you can see where the values are set and that can help you. I hope that helps. There is probbaly a couple of other changes you could make as well but nothing too major, in my opinion. def getnum1(a): a = input("What is the first number?") if a == 0: getnum1(a) return a def getnum2(b): b = input("What is the second number?") if b == 0: getnum2(b) return b def euclid(num1, num2, num3, num4): print 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 b = 0 a=getnum1(a) b=getnum2(b) x, y = 2, 100 euclid(a, b, x, y) Jul 18 '05 #3

 P: n/a > 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 Jul 18 '05 #4

 P: n/a John, If you really want a var in a function to be a global version, you need to use the "global" stmt. a = 4 def foo(): global a = x It's probably a good idea to put all global vars at the top of your file right after your import stmts. wes John wrote: I am new to using Python. Everytime I run this program it prints "The lowest common factor is 0", no matter what numbers I use. Can anybody see anything wrong with my program? Thanks in advance. def getnum1(a): a = input("What is the first number?") if a == 0: getnum1(a) def getnum2(b): b = input("What is the second number?") if b == 0: getnum2(b) 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) Jul 18 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.