Python Project – Guess the Number Game

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Program 1

# Guess Number Game
import random
def guess_number():
    print(".........Welcome in Guess  the Number Game...... ")  
    comp_choice=random.randint(1,100)
    print("I have selected a number (1-100) and if you guess this number in 10 attempts , you are Winner !")
    count=10
    i=0
    while(True):
        count-=1
        i=i+1
        user_choice=int(input("Enter your guess:  "))
        if(user_choice<comp_choice):
            print("Your number is less.. ")
        elif(user_choice>comp_choice):      
            print("Your number is grater.. ") 
        else:   
            str="Congrats You are Winner in {} Attempts : "
            str=str.format(i)
            print(str)    
            break
        if(count==0): 
            print("Oops you lose the game... !")
            print("Computer Seleted: ", comp_choice)
            break
        else:
            print("Attempts remaing: ",count) 
    choice=input("You wan to play more(yes/no): ").lower()

    if(choice=='yes'):
        guess_number()
    else:
        print("Ok Thanks for playing.. Bye Bye ")        

#Main Calling
guess_number()

 

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *