In: Computer Science
I need to implement a code in python to guess a random number. The number must be an integer between 1 and 50 inclusive, using the module random function randint. The user will have four options to guess the number. For each one of the failed attempts, the program it should let the user know whether is with a lower or higher number and how many tries they have left. If the user guess the number, the program must congratulate the user and tell him how much he won. If you are a user, hit on the 1st attempt He wins first $ 100, on the second $ 75, on the third $ 50, on the fourth $ 25. If the user misses all four opportunities, the program must inform the user that he owes money $ 60 and what was the number that the computer generates randomly.
I will give the code , also the screen shot of code and output:
code:
import random
hiddenNumber = random.randint(1,50)
won = False # to check if the user won or not
number_of_gusses = 0
for i in range(4): # number of gusses is 4
n = int(input("Gussess the number : "))
if n == hiddenNumber:
won = True
if i == 0:
amt = 100
elif i == 1:
amt = 75
elif i == 2:
amt = 50
else:
amt = 25
print("Congratulations! You gusses the number in ",i+1," attempts and won $",amt)
break
else:
if hiddenNumber > n:
print("The entered number is smaller than hidden number. Try again,")
else:
print("The entered number is greater than hidden number. Try again,")
print("Tries left :",4-i-1)
if not won:
print("Sorry!! You did not managed to guess the number . You had reached the gussessing limit.")
print("You owes money $ 60 ,The correct number is : ",hiddenNumber)
Screenshot of outputs:
I had managed to find a number by gussing:
Ouput case , where i was not able to get the correct result in 4 attempts:
Screenshot of code:
----------------------------------------------------------------------------------------------------------------
Note :Refer to the screenshot of the code to understand the indentation of the code.
Comment below, if you need to clear any more doubts regarding this question. Please upvote if you are happy. Thank you sir.