| | |
number game
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
Here's a hint:
Update possible_guesses every time a guess is made, eliminating the numbers that are greater than or less than the last guess.
Python Syntax (Toggle Plain Text)
guess = random.choice(possible_guesses)
•
•
Join Date: Nov 2008
Posts: 56
Reputation:
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 <ENTER> to exit.")
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 <ENTER> to exit.")
Last edited by dseto200; Nov 19th, 2008 at 5:09 pm.
•
•
Join Date: Nov 2008
Posts: 56
Reputation:
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 <ENTER> to exit.")
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.")
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
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]
[code=Python] ......code goes here....
....and here....
[/code]
•
•
•
•
Resolved it had to change the location of tries=1
Python Syntax (Toggle Plain Text)
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.")
•
•
Join Date: Nov 2008
Posts: 56
Reputation:
Solved Threads: 0
Testing with code tags
Python Syntax (Toggle Plain Text)
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.")
![]() |
Similar Threads
- How do i create a simple game using c++?? (C++)
- Number Game- Reset, instructions, ehancements! (VB.NET)
- Guess Number Game (Problem) (Java)
- The Number Game (Software Development Job Offers)
- Help with random number gen (C++)
Other Threads in the Python Forum
- Previous Thread: Which gui toolkit and why??
- Next Thread: Absurd Loop Problem
Views: 2362 | Replies: 19
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied address ansi backend beginner changecolor class code conversion coordinates copy curves customdialog dan08 dictionary directory dynamic edit examples excel feet file float font format ftp function generator getvalue gui halp homework i/o images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive schedule screensaverloopinactive scrolledtext searchingfile server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython





