Problem 1 Single Round Guessing Game Write a Python program
Solution
from random import randint #importing randint to generate random number
secret = randint(0, 100) #random number between 0 and 100
#display stateemnts on the screen to welcome and output the secret
print \"Welcome to the guessing game!\"
print \"-\"*50
print \"I have chosen a secret number (and it is \"+str(secret)+\").\"
#taking guess as an input from the user
guess = input(\"Take a guess : \")
#initialise the number of guesses taken to get to the right number
number_of_guesses = 1
#run the loop till the user doesn\'t enter the correct number
while guess!=secret:
#check if guess less than or greater than secret
if guess<secret:
print \"Sorry, that is too low. Guess again : \"+str(secret)
else:
print \"Sorry, that is too high. Guess again : \"+str(secret)
#taking another input from the user
guess = input(\"Take a guess : \")
#if input = -1, terminating the program
if guess == -1:
break
number_of_guesses += 1
if guess==secret:
print \"Yes that is right!\"
#printing out the number of guesses
print \"It took you \"+str(number_of_guesses)+\" guesses.\"
