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


Groups > comp.lang.python > #38893 > unrolled thread

How would you do this?

Started byeli m <techgeek201@gmail.com>
First post2013-02-14 15:34 -0800
Last post2013-02-15 11:17 -0800
Articles 6 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#38893 — How would you do this?

Fromeli m <techgeek201@gmail.com>
Date2013-02-14 15:34 -0800
SubjectHow would you do this?
Message-ID<58be0de9-e39e-4b95-99da-d8eb3270db33@googlegroups.com>
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?

[toc] | [next] | [standalone]


#38894

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2013-02-15 00:09 +0000
Message-ID<mailman.1790.1360887006.2939.python-list@python.org>
In reply to#38893
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

[toc] | [prev] | [next] | [standalone]


#38896

Fromeli m <techgeek201@gmail.com>
Date2013-02-14 16:19 -0800
Message-ID<3df34331-509e-4186-bab1-1d4536bc521f@googlegroups.com>
In reply to#38894
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. 
        

[toc] | [prev] | [next] | [standalone]


#38897

Fromeli m <techgeek201@gmail.com>
Date2013-02-14 16:19 -0800
Message-ID<mailman.1792.1360890057.2939.python-list@python.org>
In reply to#38894
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. 
        

[toc] | [prev] | [next] | [standalone]


#38950

Fromvduncan80@gmail.com
Date2013-02-15 11:17 -0800
Message-ID<4d654143-8aee-4d77-9fd7-5221d505c748@googlegroups.com>
In reply to#38897
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


[toc] | [prev] | [next] | [standalone]


#38951

Fromvduncan80@gmail.com
Date2013-02-15 11:17 -0800
Message-ID<mailman.1838.1360955886.2939.python-list@python.org>
In reply to#38897
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


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web