954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

number game

#Guess my number

#The computer picks a random number between 1 and 100
#the player tries to guess it an dhte computer lets
#the player know if the guess is too hight, too low
#or right on the money

import random

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible. \n"

#set the initial values

the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1

#guessing loop

while (guess != the_number):
if (guess > the_number)
print "Lower..."
else:
print "Higher..."

guess = int(raw_input("Take a guess: "))
tries += 1

print "You guessed it! the number was", the_number
print "And it only took you", tries, "tries!\n"

#this is the end

#this is a copy of the script run on python
>>> #Guess my number
...
>>> #The computer picks a random number between 1 and 100
... #the player tries to guess it an dhte computer lets
... #the player know if the guess is too hight, too low
... #or right on the money
...
>>> import random
>>>
>>> print "\tWelcome to 'Guess My Number'!"
Welcome to 'Guess My Number'!
>>> print "\nI'm thinking of a number between 1 and 100."

I'm thinking of a number between 1 and 100.
>>> print "Try to guess it in as few attempts as possible. \n"
Try to guess it in as few attempts as possible.

>>>
>>> #set the initial values
...
>>> the_number = random.randrange(100) + 1
>>> guess = int(raw_input("Take a guess: "))
Take a guess: tries = 1
Traceback (most recent call last):
File "", line 1, in
ValueError: invalid literal for int() with base 10: 'tries = 1'
>>>
>>> #guessing loop
...
>>> while (guess != the_number):
... if (guess > the_number)
File "", line 2
if (guess > the_number)
^
SyntaxError: invalid syntax
>>> print "Lower..."
File "", line 1
print "Lower..."
^
IndentationError: unexpected indent
>>> else:
File "", line 1
else:
^
IndentationError: unexpected indent
>>> print "Higher..."
File "", line 1
print "Higher..."
^
IndentationError: unexpected indent
>>>
>>> guess = int(raw_input("Take a guess: "))
Take a guess: tries += 1
Traceback (most recent call last):
File "", line 1, in
ValueError: invalid literal for int() with base 10: 'tries += 1'
>>>
>>> print "You guessed it! the number was", the_number
You guessed it! the number was 18
>>> print "And it only took you", tries, "tries!\n"
And it only took you
Traceback (most recent call last):
File "", line 1, in
NameError: name 'tries' is not defined

Running Ubuntu Gutsy gibbons, and emacs
I have checked the code carefully but can't get past the first traceback error.
Thanks for any help.

VinceW
Newbie Poster
1 post since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

Use code tags, please.

And try declaring tries as an integer at the start of your application.

linux
Posting Shark
933 posts since Aug 2006
Reputation Points: 118
Solved Threads: 30
 

This is the code -
import random
print "\tWelcome to 'Guess my number'!:"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible.\n"

#set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1

# guessing loop

while (guess != the_number):
if (guess > the_number):
print "Lower..."
else:
print "Higher..."

guess = int(raw_input("Take a guess: "))
tries += 1

print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"

raw_input ("\n\nPress the enter key to exit.")

Now the problem is how do you limit the amount of guesses.

dseto200
Junior Poster in Training
56 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Use code tags:
[code=python]
#Code in here
[/code]
This will make your code readable for us forum members, which in turn helps us to solve your problem.

In order to limit the number of guesses, you should be using an if guesses == allowed_guesses: statement, or something similar.

jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
 

I set the amount of tries to less than 3 ,however when I make more than 3 guesses it won't up the message. What did I do wrong?

# Guess My Number
# The computer picks a random number

import random
print "\tWelcome to 'Guess my number'!:"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible.\n"

#set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1

# guessing loop

while (guess != the_number):
    if (guess > the_number):
        print "Lower..."
    else:
        print "Higher..."
            
    guess = int(raw_input("Take a guess: "))
if  tries > 3:
    print "You have more than 3 attempts, game over."
    tries += 1
else:
    print "You guessed it! The number was", the_number
    print "And it only took you", tries, "tries!\n"

raw_input ("\n\nPress the enter key to exit.")

Editor's note:
Please use code tags with your code to preserve the indentations. Otherwise the code is very difficult to read and not too many folks will help.[code=python]
your Python code here
[/code]

dseto200
Junior Poster in Training
56 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

I set the amount of tries to less than 3 ,however when I make more than 3 guesses it won't up the message. What did I do wrong?

# Guess My Number
# The computer picks a random number

import random
print "\tWelcome to 'Guess my number'!:"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible.\n"

#set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1

# guessing loop

while (guess != the_number):
    if (guess > the_number):
        print "Lower..."
    else:
        print "Higher..."
            
    guess = int(raw_input("Take a guess: "))
if  tries > 3:
    print "You have more than 3 attempts, game over."
    tries += 1
else:
    print "You guessed it! The number was", the_number
    print "And it only took you", tries, "tries!\n"

raw_input ("\n\nPress the enter key to exit.")

I cannot read your code without code tags!

Change the loop towhile True. Omit the first guess. Initialize tries to 0, and increment tries after guess. Modify your if/else to if/elif/else. The else is for a correct guess --> break. If the user made 3 guesses --> break.

import random
print "\tWelcome to 'Guess my number'!:"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible.\n"

the_number = random.randrange(1,101)

tries = 0

while True:
    guess = int(raw_input("Take a guess: "))
    tries += 1
    if guess > the_number:
        print "Lower..."
    elif guess < the_number:
        print "Higher..."
    else:
        print "You guessed it! The number was", the_number
        print "And it only took you", tries, "tries!\n"
        break
    if  tries == 3:
        print "You have made 3 attempts, game over."
        break

raw_input ("\n\nPress the enter key to exit.")
solsteel
Junior Poster
145 posts since Mar 2007
Reputation Points: 86
Solved Threads: 42
 

Oops! Accidental duplicate post.

solsteel
Junior Poster
145 posts since Mar 2007
Reputation Points: 86
Solved Threads: 42
 

Thanks only had to make 1 correction.
Had to set
If tries > 3:
Because
If tries = = 3: it would only allow up to 2 guesses.

dseto200
Junior Poster in Training
56 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Are you referring to the code I posted?. Variable tries is equal to 3 after 3 guesses. Are you going to allow 4 guesses?

solsteel
Junior Poster
145 posts since Mar 2007
Reputation Points: 86
Solved Threads: 42
 
Are you referring to the code I posted?. Variable tries is equal to 3 after 3 guesses. Are you going to allow 4 guesses?

Hi Solsteel, Yes i'm referring to the code you posted, because when I inserted the code - if tries == 3 it would only allow 2 guesses.
When I set it to tries > 3 then it would allow 3 guesses.
Are you getting a different result?

dseto200
Junior Poster in Training
56 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Think about it. tries starts out as 0. Make a guess. tries is incremented to 1.
guess - 2
guess - 3

That's 3 guesses! How could it only be 2? Remember, I omitted the guess outside of the loop in the code I posted. It must still be in the code you tested.

solsteel
Junior Poster
145 posts since Mar 2007
Reputation Points: 86
Solved Threads: 42
 

I found the mistake my tries was set to tries = 1 it should be tries = 0.
Thanks for all your help solsteel much appreciated.

dseto200
Junior Poster in Training
56 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

You are very welcome!

solsteel
Junior Poster
145 posts since Mar 2007
Reputation Points: 86
Solved Threads: 42
 

Confirmed it works.

Now I have to figure how to modify the program when player picks a random number and the computer will have to guess my random number.

dseto200
Junior Poster in Training
56 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Here's a hint:

guess = random.choice(possible_guesses)


Updatepossible_guesses every time a guess is made, eliminating the numbers that are greater than or less than the last guess.

solsteel
Junior Poster
145 posts since Mar 2007
Reputation Points: 86
Solved Threads: 42
 

Is this the only way to solve this problem. Looking at the chapter i'm reading "Python Programming for the absolute Beginner" they don't even list that command. If it is I have to complain to the author on why he didn't even mention this command.

dseto200
Junior Poster in Training
56 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Here's the program for computer guessing my number unfortunately when I run the program
it's off by 1. For example I enter my number 24 computer guessed it correctly on the 3rd attempt.
Unfortunately it list "And it only took two tries".
I set tries=1.
If I set tries=0 it will always list it as "And it only took 1 tries".


import random
print "\t\t\tWelcome to \"Guess My Number\"!"
print "\nThink of a number between 1 and 100."
print "I will try to guess it in as few attempts as possible.\n"

number = input ("Enter the number: ")

#the computer guesses the number using the random function
guess = random.randrange (1,101)
tries = 1

#FIXME
while (guess != number):
if (guess > number):
print "You chose", guess, "the number is Lower ..."
guess = random.randint(1, guess-1)

else:
print "You chose", guess, "the number is Higher ..."
guess = random.randint(guess+1, 101)
tries += 1

print "You guessed it! The number was", number
print "And it only took you", tries, "tries!\n"
break
raw_input ("Press to exit.")

dseto200
Junior Poster in Training
56 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Resolved it had to change the location of tries=1

import random
print "\t\t\tWelcome to \"Guess My Number\"!"
print "\nThink of a number between 1 and 100."
print "I will try to guess it in as few attempts as possible.\n"

number = input ("Enter the number: ")

#the computer guesses the number using the random function
guess = random.randrange (1,501)
tries = 1

#FIXME
while (guess != number):
tries += 1
if (guess > number):
print "You chose", guess, "the number is Lower ..."
guess = random.randint(1, guess+1)

else:
print "You chose", guess, "the number is Higher ..."
guess = random.randint(guess-1, 501)

print "You guessed it! The number was", number
print "And it only took you", tries, "tries!\n"
break
raw_input ("Press to exit.")

dseto200
Junior Poster in Training
56 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

What is the hang-up you have with CODE TAGS? Your code would look like below, and other people may actually read it!

[code=Python] ......code goes here....
....and here....
[/code]

Resolved it had to change the location of tries=1

import random
print "\t\t\tWelcome to \"Guess My Number\"!"
print "\nThink of a number between 1 and 100."
print "I will try to guess it in as few attempts as possible.\n"

number = input ("Enter the number: ")

#the computer guesses the number using the random function
guess = random.randrange (1,501)
tries = 1

#FIXME
while (guess != number):
  tries += 1  
  if (guess > number):
      print "You chose", guess, "the number is Lower ..."
      guess = random.randint(1, guess+1)
          
  else:
       print "You chose", guess, "the number is Higher ..."
       guess = random.randint(guess-1, 501)
       

       print "You guessed it! The number was", number
       print "And it only took you", tries, "tries!\n"
       break
raw_input ("Press <ENTER> to exit.")
solsteel
Junior Poster
145 posts since Mar 2007
Reputation Points: 86
Solved Threads: 42
 

Testing with code tags

import random
print "\t\t\tWelcome to \"Guess My Number\"!"
print "\nThink of a number between 1 and 500."
print "I will try to guess it in as few attempts as possible.\n"

number = input ("Enter the number: ")

#the computer guesses the number using the random function
guess = random.randrange (1,501)
tries = 1

#FIXME
while True:
  guess != number
  tries += 1  
  if (guess > number):
      print "You chose", guess, "the number is Lower ..."
      guess = random.randint(1, guess+1)
          
  else:
       print "You chose", guess, "the number is Higher ..."
       guess = random.randint(guess-1, 501)
       

       print "You guessed it! The number was", number
       print "And it only took you", tries, "tries!\n"
       break
       raw_input ("\n\nPress the enter key to exit.")

       if tries == 20:
         print "You have more than 20 guesses, game over."
         break
raw_input ("\n\nPress the enter key to exit.")
dseto200
Junior Poster in Training
56 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You