Discussion:
[Tutor] Query regarding loop problem
Martin Mwaka
2015-08-29 21:37:07 UTC
Permalink
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.")
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-30 00:13:08 UTC
Permalink
Post by Martin Mwaka
myNumber = input("Input a number between 1 and 5: ")
myNumber is now a *string* representing a number from 1-5.
Post by Martin Mwaka
computerNumber = random.randint(1,5)
computerNumber is now a random *integer* between 1-5

A string can never equal an integer. "5" is not the same as 5
Types are important in programming.
Post by Martin Mwaka
print ("Your chosen number is ", myNumber,": Computer number is: ",
computerNumber)
So the else will never get called

You need to convert your string into an integer using int()

myNumber = int(input("Input a number between 1 and 5: "))


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Cameron Simpson
2015-08-30 00:16:51 UTC
Permalink
Post by Martin Mwaka
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.
[...]
Post by Martin Mwaka
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: ")
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: ")
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 <***@zip.com.au>

In theory, there is no difference between theory and practice.
In practice, there is. - Yogi Berra
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-08-30 09:38:09 UTC
Permalink
Alan has pointed out the string/int problem with your code.
Your code has a different problem, as well.
Post by Martin Mwaka
# User enters a number between 1 - 5
# Computer generates random number until an equivalent number is achieved
import random
computerNumber = 0
Ok, now you have set computerNumber to 0
Post by Martin Mwaka
computerNumber = input("Press enter to prompt the computer to enter a
number: ")
As an aside -- what happens now it I type 0<return> ?
as your code stands now, nothing, since computerNumber would
then be set to the string "0", which isn't the same as the
integer 0. But once you get your types to match, this could
be a problem for you.
This is the line that is dodgy. computerNumber isn't 0 to
start with (good, we will run this loop, as intended) but
where does computerNumber get changed?
Post by Martin Mwaka
computerNumber = random.randint(1,5)
Here. Where it will become something in [1, 2, 3, 4, 5]
But none of these are 0s. So your while loop will never terminate.

You continue
Post by Martin Mwaka
prompt = input("Press enter to prompt the computer to enter a number: ")
You don't use this anywhere, so this is just so you can single step
through your loop, I guess. If that is the idea, then it is better
to write it like this: (I shortened it to fit on one line):

prompt = input("Press enter to prompt the computer: ")
computerNumber = random.randint(1,5)

Hit return, and get the assignment. Not 'hit return and the next
time the loop is executed, if this happens, then sometime a
new number will be assiged'.

But back to the larger issue.

How are you going to get out of your while loop?

There are 3 different approaches you can use here.

The first way is to assign computerNumber to 0 when you have
finally found your match. The next time around the loop, the
while condition will fail, and the loop will end.

The second way is to explicitly break out of your loop when
you have found what you are looking for with a break statement.

The second way suggests a way to improve your code.
Instead of messing around with assigning values that can never
be True, which puts a conceptual load on the person reading your
code to trace every possible way that you could get computerNumber
to become 0 -- which, admittedly isn't a huge load in such a
tiny program.

If instead you write this as:

while True:
if <something>:
do stuff
else:
announce_victory
break

People who see 'while True' at the top know, for certain, that you
don't want this loop to ever terminate. Thus you are relying on
break statements to leave, rather than 'I want to leave via the
break statement or if computerNumber becomes 0'. Much easier to
read and understand.

The final way to get out of the loop is to replace the condition,
not with the somewhat confusing 'something that is never going
to happen normally', not with 'somethting that is very clear about
that it is never going to happen, ever' but with a test that you
are really interested in. In this case, it is

while myNumber != computerNumber:

Hope this helps,
Laura


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-09-01 05:31:02 UTC
Permalink
I have been told that in gmail it is simple, but far from obvious
to get your mail to open so you add to the bottom. There are three
tiny dots which conceal the previous text. Click on that, and then
add below. I am also told that if you open your mail message with
'control A' (hold the control key down and press A) this will also
work.

So we can see if that works ...

(Thanks to Rustom Mody, who as a gmail user ought to know such things. :) )

Laura
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Loading...