473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

New user needs help

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 1245
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
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" <om*******@invalid.email.info> 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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Simon | last post by:
I'm doing a survey. When do you think GNU/Linux will be ready for the average Joe? What obstacles must it overcome first?
3
by: Martin Lacoste | last post by:
Wondering if people have suggestions as to good reference sources for Access 2000 for an (almost..) intermediate user? It appears as though books along the lines of 'Access for Dummies' are way...
10
by: Sorin Dolha [MCSD .NET] | last post by:
I would like to start a process from C# code as another user. The C# code is executed as the ASPNET user because it relies in a Web Page class, and I would like that the process will run as another...
3
by: Maellic | last post by:
Hi, The website I am working on is built with ASP.NET and connects to a SQL Server 2000 database. The web server and database are on the same machine. I have recently tried to modify the timeout...
10
by: Sorin Dolha [MCSD .NET] | last post by:
I would like to start a process from C# code as another user. The C# code is executed as the ASPNET user because it relies in a Web Page class, and I would like that the process will run as another...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.