New user needs help

J

John

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)
 
O

omission9

John said:
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)
 
W

Wayne Folta

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.
 
J

John

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.
 
W

wes weston

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Syntax error and im a beginner 1
Calling Values 11
How to use Densenet121 in monai 0
Tkinter GUI Error 8
Min max program 10
problem in Loop 4
Help with display placement 4
sob..Someone can help me????plsss 40

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top