# [Tutor] Guessing Game Program

Malcolm Newsome malcolm.newsome at gmail.com
Sun Mar 27 03:48:34 CEST 2011

```I noticed an error in my code...and got a cool tip on how to add an "out of
range" response if the user types in a number outside of 1-100.

Enjoy!

Malcolm

# guess.py
# a simple number guessing game

import random

#---------------------------------------------------------------------------
-------------------
def nope_message(random_num):
return "Nope! I'm smarter than you!\nI was thinking of the number: %d" %
int(random_num)

def right_message(retried=False):
message = "That was right! I guess you ARE smarter than me"

if retried:
message += "... even though it took you another try!"

return message

return int(input("You were too %s. Type another number: " % message))

def retry(message, random_num):

if guess_iflow == random_num:
return right_message(True)
else:
return nope_message(random_num)

#---------------------------------------------------------------------------
------------------

def main():
print "Do you think you're smarter than me?"
print "I guess we'll see!"
print "I'm thinking of a number between 1 - 100.  Can you guess what it
is?"

random_num = random.randint(1, 100)

while True:

guess = int(input("Type a number between 1 - 100: "))
if guess < 1 or guess  > 100:
print "%s is out of range try again" % guess
continue

break

if guess == random_num:
print right_message()
elif guess < random_num:# user gets second chance if number is too low
print retry("low", random_num)
elif guess > random_num:# user gets second chance if number is too high
print retry("high", random_num)
else:
print nope_message(random_num)

if __name__ == "__main__":
main()

-----Original Message-----
From: Donald Bedsole [mailto:drbedsole at gmail.com]
Sent: Thursday, March 24, 2011 11:54 PM
To: Malcolm Newsome
Subject: Re: [Tutor] Guessing Game Program

Hi Malcolm :-)

On Fri, Mar 25, 2011 at 12:37 AM, Malcolm Newsome
<malcolm.newsome at gmail.com> wrote:
> Hey Don!
>
> I posted an eerily similar request to another python group about two
> weeks ago!  I, too, am very new to programming and the guessing game
> was my first shot at writing a script from scratch!

I got interested in writing a guessing game because I was trying to fix a
C++ program that wouldn't compile with g++ because they were using a
non-standard randomizer() function.  (I really don't know C++, but I thought
trying to fix the problems with someone else's program might help me to
learn).  I didn't make much headway in understanding how to generate random
numbers in C++, but it made me curious about how to do it in Python.  Python
seems much easier!

>
> Below is my code (improved with some help from others).  I still would
> like to make some improvements to it also.  But, perhaps there will be
> some ideas in it that can help you as well!  Looking forward to learning
and growing!
>
> All the best!
>
> Malcolm

Thanks for posting your code.  I will look at it later (closing in on
1:00 AM here) to see what I can learn from it.  Tutorials are great, but it
seems looking at code makes it easier for me to learn.

Thanks for taking the time to post, and I hope you're successful in your
programming studies.

Don

```