Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; 'output': 0.04; 'sys': 0.05; 'except:': 0.07; 'try:': 0.07; 'subject:How': 0.09; 'python': 0.09; 'err:': 0.09; 'subject:would': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr :python-list': 0.10; 'def': 0.10; 'itself.': 0.11; '"no":': 0.16; '"yes":': 0.16; 'benjamin': 0.16; 'cc:name:python list': 0.16; 'cheers!': 0.16; 'comp': 0.16; 'guessing': 0.16; 'replaces': 0.16; 'wrote:': 0.17; 'tries': 0.17; 'input': 0.18; 'import': 0.21; 'cc:2**0': 0.23; 'raise': 0.24; 'random': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; '(which': 0.26; 'guess': 0.27; 'question': 0.27; 'this?': 0.28; 'received:209.85.212': 0.28; 'run': 0.28; 'boundary': 0.29; 'convert': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'thursday,': 0.30; 'sense': 0.31; 'code': 0.31; 'print': 0.32; 'skip:s 30': 0.33; 'code:': 0.33; 'received:google.com': 0.34; 'follows:': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'except': 0.36; 'explain': 0.36; 'but': 0.36; "didn't": 0.36; 'uses': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'help': 0.40; 'think': 0.40; 'your': 0.60; 'from:no real name:2**0': 0.60; 'you.': 0.61; 'between': 0.63; 'more': 0.63; 'show': 0.63; '100': 0.78; 'subject:this': 0.84; '2.7.': 0.84; '2013': 0.84; 'oscar': 0.84; 'played': 0.84; 'subject:you': 0.88; 'eli': 0.93; 'eliminates': 0.93 X-Received: by 10.50.180.197 with SMTP id dq5mr348086igc.17.1360955878354; Fri, 15 Feb 2013 11:17:58 -0800 (PST) Newsgroups: comp.lang.python Date: Fri, 15 Feb 2013 11:17:57 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=174.45.250.221; posting-account=pYBz4QoAAAAYhcbjWSf3Hi4p-eBqe9dK References: <58be0de9-e39e-4b95-99da-d8eb3270db33@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 174.45.250.221 MIME-Version: 1.0 Subject: Re: How would you do this? From: vduncan80@gmail.com To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 181 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360955886 news.xs4all.nl 6943 [2001:888:2000:d::a6]:45605 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38951 On Thursday, February 14, 2013 5:19:51 PM UTC-7, eli m wrote: > On Thursday, February 14, 2013 4:09:37 PM UTC-8, Oscar Benjamin wrote: >=20 > > On 14 February 2013 23:34, eli m wrote: >=20 > >=20 >=20 > > > I want to make a guess the number game (Which i have), but i want to = make the computer play the game against itself. How would i do this? >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > Your question would make more sense if you would show your program and >=20 > >=20 >=20 > > also explain how you would like the output to look when the computer >=20 > >=20 >=20 > > played itself. >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > Oscar >=20 > This is my code: >=20 >=20 >=20 > #Guess the number game >=20 > import random >=20 > run =3D 0 >=20 > while run =3D=3D 0: >=20 > print ("I am thinking of a number between 1 and 100") >=20 > num =3D random.randint(1, 100) >=20 > num =3D int(num) >=20 > guesses =3D 0 >=20 > guessestaken =3D 0 >=20 > while guesses =3D=3D 0: >=20 > try: >=20 > guess =3D raw_input("Your guess:") >=20 > guess =3D int(guess) >=20 > guessestaken =3D (guessestaken) + 1 >=20 > guessestaken =3D int(guessestaken) >=20 > if guess =3D=3D (num): >=20 > print 'Correct! It took you', int(guessestaken), 'guesses= !' >=20 > playagain =3D raw_input("Do you want to play again?") >=20 > if playagain =3D=3D "yes": >=20 > guesses =3D 1 >=20 > if playagain =3D=3D "no": >=20 > run =3D 1 >=20 > if guess > num: >=20 > print ("My number is lower") >=20 > if guess < num: >=20 > print ("My number is higher") >=20 > except TypeError, err: >=20 > print ("Not a valid number") >=20 > =20 >=20 > I would like it to show the computer guessing the numbers. Hello. I think you code is Python 2.7. My solution uses Python 3 but I ca= n help you convert it if the solution is what you are looking for. My appr= oach as to create a class that tries to guess the right number. This code = also eliminates raw_input. I didn't know how important having it respond v= ia raw_input is to you. Code follows: import random import sys class Guesser(): def __init__(self): self.low =3D 1 self.high =3D 100 =20 def getRand(self,x,y): num =3D random.randint(x,y)=20 return num =20 =20 def guess(self,guess,boundary): if boundary =3D=3D ">": self.low =3D guess elif boundary =3D=3D "<": self.high =3D guess else: self.low =3D 1 self.high =3D 100 return self.getRand(self.low,self.high) =20 def playagain(self): choice =3D ['Y','N'] return random.choice(choice) =20 run =3D 0 while run =3D=3D 0:=20 guess=3D1 guesses=3D0 guessestaken =3D 0 comp =3D Guesser() num =3D comp.getRand(1,100)=20 result =3D "" print ("I am thinking of a number between 1 and 100")=20 while guesses =3D=3D 0:=20 guessestaken +=3D 1 try:=20 guess =3D comp.guess(guess,result) # replaces input except: =20 print("Unexpected error:", sys.exc_info()[0])=20 raise =20 print("Your guess:", guess) =20 if guess =3D=3D num:=20 print('Correct! It took you', guessestaken, 'guesses!')=20 guesses =3D 1 elif guess > num: print("My number is lower")=20 result =3D "<" else:=20 print("My number is higher")=20 result =3D ">" print("Do you want to play again?")=20 playagain =3D comp.playagain() # replaces input print(playagain) if playagain =3D=3D "N":=20 run =3D 1 Please let me know if you have questions or would like to discuss this solu= tion further. Cheers! vduncan