20 Questions(Python 2.6)

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

20 Questions(Python 2.6)

Post by Arrexel on Fri Feb 20, 2009 9:07 pm
([msg=18331]see 20 Questions(Python 2.6)[/msg])

I am well aware that there is an excess amount of code, or code that could be written more efficiently. Was just learning definitions at the time, so why not use it for a loop? I just threw this together in an attempt to end my teacher's reign of python-crashing terror. When he entered:

Code: Select all
Please enter your first guess(between 1 and 500):
>>> if E == mc2 + thisprogramwilldie!!!!!: THEEND = True


And found that his guess was in fact, 2 (the only valid entry) that he was quite far off. That was his exact input, char for char.

Code: Select all
import random
import re

def main():
    print "Welcome to 20 Questions.\nYou must guess the correct number.\nYour number is between 1 and 500.\n"

    counter = 20
    number = random.randrange(1, 500)
   
    guess = raw_input("Please enter your first guess(between 1 and 500): ")
    guess = re.sub('[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-/:;<=>.?@[\\]^_`{|}~\t\n\x0b\x0c\r]', '', guess)

    while counter > 0:
        while guess == "":
            guess = raw_input("Please enter a valid interger between 1 and 500: ")
            guess = re.sub('[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-/:;<=>.?@[\\]^_`{|}~\t\n\x0b\x0c\r]', '', guess)

        if guess == "":
            guess = ""
               
       
        if int(guess) == number:
            print ""
            print "Congratulations, you guessed the correct number!"
            print ""
            print ""
            print ""
            guess = 0
            counter = 20
            int(number = random.randrange(1, 500))
            main()

        elif int(guess) > number:
            counter = counter - 1
            print ""

            if counter > 0:
                print "You guessed too high.",counter,"guesses remaining."
                guess = raw_input("Please enter your next guess: ")
                guess = re.sub('[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-/:;<=>.?@[\\]^_`{|}~\t\n\x0b\x0c\r]', '', guess)

            elif counter == 0:
                print "You are out of guesses."

        elif int(guess) < number:
            counter = counter - 1
            print ""
           
            if counter > 0:
                print "You guessed too low.",counter,"guesses remaining."
                guess = raw_input("Please enter your next guess: ")
                guess = re.sub('[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-/:;<=>.?@[\\]^_`{|}~\t\n\x0b\x0c\r]', '', guess)

            elif counter == 0:
                print "You are out of guesses."
           
        if counter == 0:
            print ""
            print "You were unable to guess the number in time."
            print "Your number was actually",number
            print ""
            print ""
            print ""
            guess = 0
            counter = 20
            int(number = random.randrange(1, 500))
            main()

    if counter == 0:
        print ""
        print "You were unable to guess the number in time."
        print "Your number was actually",number
        print ""
        print ""
        print ""
        guess = 0
        counter = 20
        int(number = random.randrange(1, 500))
        main()

main()

       
   


-- Sat Feb 21, 2009 10:49 am --

I had an idea this morning, for some odd reason, to make this a challenge to people learning Python. Take my script and change it to your heart's content. I want to see who can come up with the most efficient, and shortest, script that they can. The script must display everything exactly the same as the original, and it must include the sub functions.

Good luck :)

-Arr
"I think there is a world market for maybe five computers."
Unverified Quote, 1945 - Thomas J. Watson, Founder of IBM
User avatar
Arrexel
New User
New User
 
Posts: 31
Joined: Fri Feb 13, 2009 7:44 pm
Location: Ontario, Canada
Blog: View Blog (0)


Re: 20 Questions(Python 2.6)

Post by ArgentPyro on Sun Mar 08, 2009 6:01 pm
([msg=19588]see Re: 20 Questions(Python 2.6)[/msg])

alright, so this is what I've come up with so far. I took it from 66 lines to 27. Only bug I can find is that when you submit just non-numeric characters, the int() function can't convert '"' to a decimal, so it crashes. but anyways, here it is:

Code: Select all
import random, re
def reply(switch, counter):
   if counter > 0:
      print "You guessed too",switch,counter,"guesses remaining."
      guess = int(re.sub('[\D]', '', raw_input("Please enter your next guess: ")))
   elif counter == 0:
      print "You are out of guesses.\n\nYou were unable to guess the number in time.\nYour number was actually",number,"\n\n\n"
   return guess
big = 1
while big == 1:
   print "Welcome to 20 Questions.\nYou must guess the correct number.\nYour number is between 1 and 500.\n"
   counter = 20
   number = random.randrange(1, 500)
   print number
   guess = int(re.sub('[\D]', '', raw_input("Please enter your first guess(between 1 and 500): ")))
   while counter > 0:
      while guess == "":
         guess = re.sub('[\D]', '', raw_input("Please enter a valid integer between 1 and 500: "))
      if guess == number:
         print "\nCongratulations, you guessed the correct number!\n\n\n"
         counter = 0
      elif guess > number:
         counter -= 1
         guess = reply("high.", counter)
      elif guess < number:
         counter -= 1
         guess = reply("low.", counter)


EDIT:A try except method could probably be used to fix the problem, but I'm too lazy to do it now. So go ahead and try it yourself if you want.
Image
ArgentPyro
New User
New User
 
Posts: 10
Joined: Mon Feb 09, 2009 5:28 pm
Blog: View Blog (0)


Re: 20 Questions(Python 2.6)

Post by aNewHobby4me on Thu Jun 18, 2009 7:19 pm
([msg=25578]see Re: 20 Questions(Python 2.6)[/msg])

Here's my stab at it. It was pretty well written to begin with, so I mostly just took out all the duplicated code. Made it down to 25 lines of code.

I've just started on python, so is using global variables considered good programming practice?


Code: Select all
import random, re
counter, number, guess, hilo = 20, random.randrange(1,500), "", {'False':'low.', 'True':'high.'}
   
def restartGame():
    global counter, number, guess
    counter, number, guess = 20, random.randrange(1,500), ""
    main()
      
def getGuess():
    myGuess = raw_input("Please enter a valid interger between 1 and 500: ")
    return re.sub('[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-/:;<>.?@[\\]^_`{|}~\t\n\x0b\x0c\r]', '', myGuess)

def main():
    global counter, number, guess
    print "Welcome to 20 Questions.\nYou must guess the correct number.\nYour number is between 1 and 500.\n"
    while counter > 0:
        while guess == "":
            guess = getGuess()
        counter -= 1
        if int(guess) == number:
            print "\nCongratulations, you guessed the correct number!\n\n\n"
            restartGame()
        else:
            print "\nYou guessed too", hilo[str(int(guess) > number)],counter,"guesses remaining."
        guess = getGuess()         
    print "\nYou are out of guesses.\nYou were unable to guess the number in time.\nYour number was actually",number,"\n\n\n"
    restartGame()
main()
"To understand recursion you must first understand recursion."
aNewHobby4me
Poster
Poster
 
Posts: 185
Joined: Thu Jan 08, 2009 5:44 pm
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests