Sunday, 4 May 2014

Guess the number game in PYTHON!!

Well, we love to guess and we feel happy if our guess is right.
This game is based on your guess. Computer randomly selects a number and you have to guess the number.
Simple!! Isn't it?? But you are give limited chances based on your range. So test your intuition with this game!!!
This game is written in python and output of the program will be as shown in figure:

The program is as follows:

import simplegui
import random
secret_number=0
secret_number1=0
secret_number2=0
remaining_100=0
remaining_1000=0
remaining=0
def new_game():
    global secret_number1,secret_number2,remaining_100,remaining_1000
    secret_number1=random.randrange(0,100)
    secret_number2=random.randrange(0,1000)
    remaining_100=7
    remaining_1000=10

def range100():
    
    global secret_number1,secret_number2,remaining_100,remaining,secret_number 
    new_game()
    secret_number=secret_number1
    remaining=remaining_100
    
def range1000():
   
    global secret_number1,secret_number2,remaining,remaining_1000,secret_number 
    new_game()
    secret_number=secret_number2
    remaining=remaining_1000
    
    
def input_guess(guess):
    global remaining,secret_number
    
    remaining=remaining - 1
    if(secret_number>int(guess)):
        print "Guess was: ",int(guess)
        print "No.of remaining choices: ",remaining
        print "Higher!!"
        
    elif(secret_number<int(guess)):
        print "Guess was: ",int(guess)
        print "No.of remaining choices: ",remaining
        print "Lower!!"
    else:
        print "Your guess was: ",int(guess)
        print "You win!!!"
    if(remaining==0):
        print "You lose!!"
        
        
frame=simplegui.create_frame("Guess the number!!!",300,300)
frame.add_input("Enter the guess",input_guess,150)
frame.add_button("Range of 0-100",range100,150)
frame.add_button("Range of 0-1000",range1000,150)
frame.start()

No comments:

Post a Comment