****CODE IN PYTHON***** (Please replicate it EXACTLY as it is shown in the sample run)
Write a program where the user and the program/computer play a number guessing game. The program should prompt the user for a number (between 1 and 100, inclusive) then the program/computer has to guess what the user entered. Keep track of the number of iterations it takes for the computer to guess the number. Sample runs are shown below:
Enter number to be guessed: 88
You entered 88, and it took the program 3 iterations to guess
Enter number to be guessed: 55
You entered 55, and it took the program 19 iterations to guess
import random
n=int(input("Enter number to be guessed:"))
if(n>=1 and n<=100):
cnt=1
while(True):
generated_value=random.randint(1,100)
if(n==generated_value):
print("You entered {}, and it took the program {} iterations to guess".format(n,cnt))
break
else:
cnt=cnt+1
else:
pass
kindly follow indentation as per the above screenshoot othwrwise python raises indentation error
#updated take input 2 times:
import random
check=0
while(check!=2):
n=int(input("Enter number to be guessed:"))
if(n>=1 and n<=100):
cnt=1
while(True):
generated_value=random.randint(1,100)
if(n==generated_value):
print("You entered {}, and it took the program {} iterations to guess".format(n,cnt))
break
else:
cnt=cnt+1
check=check+1
else:
pass
Get Answers For Free
Most questions answered within 1 hours.