New user needs help

Discussion in 'Python' started by John, Feb 21, 2004.

  1. John

    John Guest

    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)
     
    John, Feb 21, 2004
    #1
    1. Advertising

  2. John

    omission9 Guest

    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)
     
    omission9, Feb 21, 2004
    #2
    1. Advertising

  3. John

    Wayne Folta Guest

    > 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
    >
     
    Wayne Folta, Feb 21, 2004
    #3
  4. John

    John Guest

    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:64CZb.19719$...
    > 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)
    >
     
    John, Feb 21, 2004
    #4
  5. John

    wes weston Guest

    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)
    >
    >
     
    wes weston, Feb 21, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. drjekil

    new user needs help!

    drjekil, Apr 8, 2008, in forum: Python
    Replies:
    0
    Views:
    287
    drjekil
    Apr 8, 2008
  2. drjekil

    new user needs help!

    drjekil, Apr 8, 2008, in forum: Python
    Replies:
    1
    Views:
    286
    Mike Driscoll
    Apr 8, 2008
  3. drjekil

    Re: new user needs help!

    drjekil, Apr 9, 2008, in forum: Python
    Replies:
    2
    Views:
    295
    Chris
    Apr 10, 2008
  4. Zayd Connor
    Replies:
    8
    Views:
    116
    Zayd Connor
    Apr 6, 2009
  5. Replies:
    2
    Views:
    467
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page