GUESS THE NUMBER (PYTHON)

 TASK-

Write a python Program for guessing a number with following conditions

  1. There should be limited number of tries.
  2. There should be a message printed as game over when all tries fail.
  3. If the number is guessed before end of tries a suitable message should be printed.
  4. After each try must show number of tries left.
  5. At last show the number of tries used if number is guessed before end of tries.

CODE:

n=18 #fixed number
c=9 #limited tries (condition 1)
while (c>0): #to fix the tries
    u=int(input("Enter the number: "))
    if (u>n):
        print("Entered number is greater!!")
        c=c-1 #condition 4
        print("You are left with",c,"tries")
    elif(u<n):
        print("Entered number is smaller!!")
        c=c-1
        print("You are left with",c,"tries")
    else:
        c=c-1
        print("You guessed correctly")#condition 3
        break
if (c==0):
    print("Game over") #condition 2
else:
    print("You guessed correct number in",9-c,"tries") #condition 5

OUTPUT:




NEED WORK PRODUCE WORK

My name is Abdul Rehman I am from Pakistan. I am doing BS in Computer and information sciences. Currently, I am creating these blogs to help students of computer sciences all over the world..

Post a Comment (0)
Previous Post Next Post