Beginner python - stuck in a loop

Posted by Jeremy on Stack Overflow See other posts from Stack Overflow or by Jeremy
Published on 2010-03-16T08:39:00Z Indexed on 2010/03/16 8:46 UTC
Read the original article Hit count: 376

Filed under:
|
|

I have two begininer programs, both using the 'while' function, one works correctly, and the other gets me stuck in a loop. The first program is this;

num=54
bob = True
print('The guess a number Game!')


while bob == True:
    guess = int(input('What is your guess?  '))

    if guess==num:
        print('wow! You\'re awesome!')
        print('but don\'t worry, you still suck')
        bob = False
    elif guess>num:
        print('try a lower number')
    else:
        print('close, but too low')

print('game over')``

and it gives the predictable output of;

The guess a number Game!
What is your guess?  12
close, but too low
What is your guess?  56
try a lower number
What is your guess?  54
wow! You're awesome!
but don't worry, you still suck
game over

However, I also have this program, which doesn't work;

#define vars
a = int(input('Please insert a number: '))
b = int(input('Please insert a second number: '))

#try a function
def func_tim(a,b):
    bob = True
    while bob == True:
        if a == b:
            print('nice and equal')
            bob = False
        elif b > a:
             print('b is picking on a!')
        else:
            print('a is picking on b!')
#call a function
func_tim(a,b)

Which outputs;

Please insert a number: 12
Please insert a second number: 14
b is picking on a!
b is picking on a!
b is picking on a!
...(repeat in a loop)....

Can someone please let me know why these programs are different? Thank you!

© Stack Overflow or respective owner

Related posts about python

Related posts about beginner