[Tutor] Query regarding loop problem

Cameron Simpson cs at zip.com.au
Sun Aug 30 02:16:51 CEST 2015


On 29Aug2015 22:37, Martin Mwaka <martinmwaka at gmail.com> wrote:
>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.
[...]
>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.")

Looks like you assign the new number to "prompt" instead of to "myNumber".

Cheers,
Cameron Simpson <cs at zip.com.au>

In theory, there is no difference between theory and practice.
In practice, there is. - Yogi Berra


More information about the Tutor mailing list