- 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



