Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38951

Re: How would you do this?

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 <vduncan80@gmail.com>
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 <mailman.1792.1360890057.2939.python-list@python.org>
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> <mailman.1790.1360887006.2939.python-list@python.org> <mailman.1792.1360890057.2939.python-list@python.org>
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 <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.1838.1360955886.2939.python-list@python.org> (permalink)
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

Show key headers only | View raw


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:
> 
> > On 14 February 2013 23:34, eli m <techgeek201@gmail.com> wrote:
> 
> > 
> 
> > > 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?
> 
> > 
> 



> > 
> 
> > 
> 
> > Your question would make more sense if you would show your program and
> 
> > 
> 
> > also explain how you would like the output to look when the computer
> 
> > 
> 
> > played itself.
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > Oscar
> 
> This is my code:
> 
> 
> 
> #Guess the number game
> 
> import random
> 
> run = 0
> 
> while run == 0:
> 
>     print ("I am thinking of a number between 1 and 100")
> 
>     num = random.randint(1, 100)
> 
>     num = int(num)
> 
>     guesses = 0
> 
>     guessestaken = 0
> 
>     while guesses == 0:
> 
>         try:
> 
>             guess = raw_input("Your guess:")
> 
>             guess = int(guess)
> 
>             guessestaken = (guessestaken) + 1
> 
>             guessestaken = int(guessestaken)
> 
>             if guess == (num):
> 
>                 print 'Correct! It took you', int(guessestaken), 'guesses!'
> 
>                 playagain = raw_input("Do you want to play again?")
> 
>                 if playagain == "yes":
> 
>                     guesses = 1
> 
>                 if playagain == "no":
> 
>                     run = 1
> 
>             if guess > num:
> 
>                 print ("My number is lower")
> 
>             if guess < num:
> 
>                 print ("My number is higher")
> 
>         except TypeError, err:
> 
>             print ("Not a valid number")
> 
>             
> 
> 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 can help you convert it if the solution is what you are looking for.  My approach 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 via raw_input is to you.  Code follows:

import random
import sys

class Guesser():
    def __init__(self):
        self.low = 1
        self.high = 100
        
    def getRand(self,x,y):
        num = random.randint(x,y) 
        return num     
       
    def guess(self,guess,boundary):
        if boundary == ">":
            self.low = guess
        elif boundary == "<":
            self.high = guess
        else:
            self.low = 1
            self.high = 100
        return self.getRand(self.low,self.high)
    
    def playagain(self):
        choice = ['Y','N']
        return random.choice(choice)
       

run = 0

while run == 0: 
    guess=1
    guesses=0
    guessestaken = 0
    comp = Guesser()
    num = comp.getRand(1,100) 
    result = ""
    print ("I am thinking of a number between 1 and 100") 
    while guesses == 0: 
        guessestaken += 1
        try: 
            guess = comp.guess(guess,result) # replaces input
        except:  
            print("Unexpected error:", sys.exc_info()[0]) 
            raise
          
        print("Your guess:", guess)    
        if guess == num: 
            print('Correct! It took you', guessestaken, 'guesses!') 
            guesses = 1
        elif guess > num:
            print("My number is lower") 
            result = "<"
        else: 
            print("My number is higher") 
            result = ">"
    print("Do you want to play again?") 
    playagain = comp.playagain()    # replaces input
    print(playagain)
    if playagain == "N": 
        run = 1

Please let me know if you have questions or would like to discuss this solution further.

Cheers!
vduncan


Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

How would you do this? eli m <techgeek201@gmail.com> - 2013-02-14 15:34 -0800
  Re: How would you do this? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-02-15 00:09 +0000
    Re: How would you do this? eli m <techgeek201@gmail.com> - 2013-02-14 16:19 -0800
    Re: How would you do this? eli m <techgeek201@gmail.com> - 2013-02-14 16:19 -0800
      Re: How would you do this? vduncan80@gmail.com - 2013-02-15 11:17 -0800
      Re: How would you do this? vduncan80@gmail.com - 2013-02-15 11:17 -0800

csiph-web