a simple number guessing game
import sys, os, time, random
loop = ["10", "9", "8" ,"7" , "6" , "5", "4", "3" , "2" , "1"]
l = random.choice(loop) # Random choice of loop
if sys.version_info[0] > 2: # fixes python 2 input as raw_input
inp = input # python3 input
else:
inp = raw_input # python2 raw_input
if "-debug" in sys.argv: print("Number is: [\033[31m%s\033[0m]" % l) # For debugging (cough) cheating xD
nc = inp("Guess the number? between 1 and 10 ") # asking for user input
if nc == l: print("yay you got %s" % l) # if user input is equal to random choice of loop print this
time.sleep(1)
os.system("clear")
if not nc == l: print("=/ you no win the number was\n [\033[31m%s\033[0m]" % l) # if user input is not equal to random choice of loop print this
time.sleep(1)
os.system("clear")
You could do
import random
print """\t\t\tWelcome to \"Guess the Number\"!
\nThink of a number between 1 and 100.
try to guess it in as few attempts as possible.\n"""
number = input ("Enter the number: ")
guess = random.randrange (100) + 1
tries = 1
useds = []
while (guess != number):
if (guess > number):
print "You chose", guess, "the number is Lower ..."
useds.append(guess)
else:
print "You chose", guess, "the number is Higher ..."
useds.append(guess)
guess = random.randrange (100) + 1
if guess in useds:
while guess in useds:
guess = random.randrange(100) + 1
else:
tries += 1
print "You guessed it! The number was", number
print "And it only took you", tries, "tries!\n"
raw_input ("Press <ENTER> to exit.")