[Tutor] random number generator
Andrew James
aijames at exemail.com.au
Fri Oct 5 07:47:13 CEST 2007
I need to start using the reply all button...
Andrew James wrote:
> while guess != number:
> guess = float(raw_input("Make another guess: "))
> if guess > number:
> print "Lower..."
> elif guess < number:
> print "Higher..."
> tries += 1
>
> You're asking people to change their choice before the while loop
> ends. Now this looks a little ugly as it will be asking people to make
> two guesses right off the bat, or it'll use the term "another" for
> their first guess. Shouldn't be too hard to change. Or do this.
>
> while guess != number:
> if guess > number:
> print "Lower..."
> elif guess < number:
> print "Higher..."
> tries += 1
> if guess != number:
> guess = float(raw_input("Make another guess: "))
>
>
>
>
> Jerry VanBrimmer wrote:
>> I'm no Python wizard, I'm still learning myself. But I think you need
>> another "if" statement to check if "guess" is equal to "number".
>>
>> if guess == number:
>> print "Congratulations!"
>>
>>
>> Something like that.
>>
>>
>>
>> On 10/4/07, Jim Hutchinson <jim at ubuntu-rocks.org> wrote:
>>
>>> Hello,
>>>
>>> I am writing a little program to test a theory and as part of teaching
>>> myself Python. I've only been at this about a week now. I have a
>>> program that "should" work but doesn't. It generates a random number
>>> between 1 and 2 out to 10 decimal places. I think there is something
>>> wrong with how my random number is generated or defined or how my
>>> guesses are defined. I added a line to tell me what the random number
>>> is and then if I enter it as a guess it doesn't match and exit the
>>> loop. Any idea what I'm doing wrong? Here is a sample output:
>>>
>>> ---
>>> I'm thinking out to 10 decimal places. Good luck.
>>>
>>> 1.14981949962
>>> Make a guess: 1.14981949962
>>> Higher...
>>> Make another guess: 1.14981949963
>>> Lower...
>>> 1.14981949963
>>> Make another guess:
>>> ---
>>>
>>> Here is my code:
>>>
>>> ---
>>> # Number guessing game
>>> #
>>> # The computer will choose a number between 1 and 2 (to ten decimal
>>> places)
>>> # and the player will try to guess the number. The program will tell
>>> the
>>> # player the number is either higher or lower than the number they
>>> guessed.
>>> import random
>>> import os
>>> os.system("clear")
>>> print "\nWelcome to 'Guess My Number'!"
>>> print "\nI'm thinking of a number between 1 and 2."
>>> print "\nYes, that's right. Between 1 and 2."
>>> print "\nYou have heard of decimals right? Well, I'm"
>>> print "\nthinking out to 10 decimal places. Good luck.\n"
>>> # set random value
>>> random.seed()
>>> number = random.random() + 1
>>> print number
>>> guess = float(raw_input("Make a guess: "))
>>> tries = 1
>>> # the guess loop
>>> while (guess != number):
>>> if (guess > number):
>>> print "Lower..."
>>> else:
>>> print "Higher..."
>>> guess = float(raw_input("Make another guess: "))
>>> tries += 1
>>> print "Congratulations! You guessed my number! The number was", number
>>> print "It took you only", tries, "tries!\n"
>>> # end
>>> ---
>>>
>>> Thanks,
>>> Jim
>>>
>>> --
>>> Please avoid sending me Word or PowerPoint attachments.
>>> See http://www.gnu.org/philosophy/no-word-attachments.html
>>> _______________________________________________
>>> Tutor maillist - Tutor at python.org
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>>
>>
>>
>>
>
More information about the Tutor
mailing list