My Code Library
Friday, November 4, 2011
Help to manually guess a randomly generated integer between 1 and 100
def guess():
import random
num=random.randint(0,100)
a=0
t=0
while a==0:
t+=1
temp=int(raw_input("Guess the number\n"))
if temp==num:
print "That's right!"
break
if temp<num:
print "the number is too small. Enter a larger number"
continue
if temp>num:
print "the number is too large. Enter a smaller number"
continue
print "You took just ",t," trials"
Here's another code for the same purpose, just a bit more user-unfriendly :P
def guess():
import random
n=1
x=random.randrange(100)
t=1
y=int(raw_input("Guess the number....its between o and 100 "))
while n==1:
if y==x:
print "Congratulations! You are right! "
break
elif y<x:
print "your number is smaller than the required number "
else:
print "your number is larger than the required number "
op=raw_input("Do you want to try again? y/n ")
if op=="y":
t=t+1
y=int(raw_input("Okay...so what's your guess? "))
continue
elif op=="n":
break
else:
print "error!goodbye.."
break
print "You have made "+str(t)+" trials"
Subscribe to:
Comments (Atom)



