Number Guessing Game using Python
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
Program 1
# Guess Number Game
import random
def guess_number():
comp_choice=random.randint(1,100)
print("-----------Guess Number Game---------------")
print("Enter your guess(1-100) and if you guess in 5 attempts You are Winner !")
count=5
i=0
while(True):
count-=1
i=i+1
user_choice=int(input("Enter Your Guess: "))
if(user_choice<comp_choice):
print("Your guess is less: ")
str="{} Attempt remaing"
str=str.format(count)
print(str)
elif(user_choice>comp_choice):
print("Your guess is greater: ")
str="{} Attempt remaing"
str=str.format(count)
print(str)
else:
str="Congrats You Win the Game in {} Attempts"
str=str.format(i)
print(str)
break
if(count==0):
print("Oops You lose the game... try again")
print("Correct Number is : ",comp_choice)
break
choice=input("You want to play more.... !(yes/no)").lower()
if(choice=='yes'):
guess_number()
else:
print("Thanks for playing .. Bye Bye")
# Main calling
guess_number()
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

