Modify the guessing-game program so that the user thinks of a number that the computer must guess.



The computer must make no more than the minimum number of guesses, and it must prevent the user from cheating by entering misleading hints.

Answer :

Answer:

Follows are the code to this question

import random as R #import randaom package

import math as M #import randaom package

S= int(input("Input smaller number: "))#defining variable and input value

L= int(input("Input larger number: "))#defining variable and input value

C= 0#defining variable and assign value

while True:#define loop

   C+= 1#count value

   num= (S+L) // 2#calculate average

   print('%d %d' % (S,L))#print value

   print('number : %d' % num)#print average

   choice = input('Input =, <, or >: ')#input value in choice variable

   if choice == '=':#define if to check value

       print("Hooray, you get %d tries" % C)#print message

       break#use break  

   elif S==L:#defining and condition to check it is equal

       print("I'm out from the guesses, and you cheat")#print message

       break#use break

   elif choice == '<':#defining another condition to check

       L = num - 1#calculate larger value

   else:#else  

       S= num + 1# calculate smaller value

Output:

please find the attached file.

Explanation:

In the code first, we import the "math and random" package, then two-variable "S and L" is defined, in which it is used to input the value from the user-end, and variable "c" is defined to count the value.

In the next step, a while loop is defined, that uses the input value and print its average vale, and the choice variable to input the symbol value and use the conditional statement, which is defined as follows:

  • In if block, if the choice is equal it will print a message.
  • In elif s is equal to l it will print message.
  • In elif, the choice is "<" it will calculate the less value and print its value, otherwise, it will go in else, in this it calculates the value of "S".
${teks-lihat-gambar} codiepienagoya

Other Questions