Friday, November 4, 2011

Sort numbers entered as a List in Python

def s(seq):
    for i in range(0,len(seq)):
          for j in range(i+1,len(seq)):
               if seq[i]>seq[j]:
                    seq[i],seq[j]=seq[j],seq[i]
    print seq

 

Generate a random number with given number of digits

def w():
    x=[0,1,2,3,4,5,6,7,8,9]
    import random
    n=int(raw_input("how many digits do u want in your number?"))
    y=" "
    for i in range(n):
        y+=str(random.choice(x))
    print y," "





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"

Getting Started: The while loop

def Lowkhyaa():
____x=int(raw_input("what is Lowkhyaa's roll number?"))
____while x!=20091068:
________x=int(raw_input("oh that's wrong! try again...."))
____print "you are absolutely right! you are a genius!"