[Tutor] Query regarding loop problem

Martin Mwaka martinmwaka at gmail.com
Sat Aug 29 23:37:07 CEST 2015


Hello

I would be grateful for some help please.  I have recently started learning
python and I am attemping to write a programme where a user enters a number
between 1 and 5, and the computer generates random numbers until an
equivalent number is achieved.  The code (see below) runs, but the else
part of the loop does not run when the computer generates an equivalent
number. I have tried to resolve this a number of ways and have run the code
under a debugger, but cannot work out why the else part is not running.  I
would be grateful for your help / guidance with this.

Many thanks

Martin

*Full code below:*

# User enters a number between 1 - 5
# Computer generates random number until an equivalent number is achieved

import random
computerNumber = 0
myNumber = input("Input a number between 1 and 5: ")
print ("Your chosen number is: ", myNumber)
computerNumber = input("Press enter to prompt the computer to enter a
number: ")


while computerNumber != 0:
    if myNumber != computerNumber:
        computerNumber = random.randint(1,5)
        print ("Your chosen number is ", myNumber,": Computer number is: ",
computerNumber)
        print ("Numbers do not match.")
        prompt = input("Press enter to prompt the computer to enter a
number: ")
    else:
        print ("MyNumber is ", str(myNumber),": Computer number is: ",
str(computerNumber))
        print ("We have a match.")


More information about the Tutor mailing list