Rational guessing of the selected number in a given range

There is a task:

The player selects a random number from 1 to 100, which the computer must guess. Before you begin, think about how you guessed it. If everything goes well, try coding the game.

My code is:

import random
num = int(input('Your number: '))
numC = random.randint(1, 100)
tries = 1
while numC != num:
    numC = random.randint(1, 100)
    if numC > num:
        print(numC, 'Less')
        numC = random.randint(1, numC)
    else:
        print(numC, 'More')
        numC = random.randint(numC, 100)
    tries += 1
print(numC, 'Computer guessed your number with', tries, 'tries')

This works, but I don’t think it works the way the author wanted.

How to make this number of the intended program used with fewer attempts? I know this is about reducing the frames of a random generator, but I don’t know how to do this without using an infinite number of variables.

+4
source share
4 answers

I would change this path to dynamically remember the range and guess in the correct range

import random
num = int(input('Your number: '))
numC = random.randint(1, 100)
tries = 1
lowerLimit=1
upperLimit=100
while numC != num:
  if numC > num:
    print(numC, 'Less')
    upperLimit = numC+1
  else:
    print(numC, 'More')
    lowerLimit = numC-1
  tries += 1
  numC = random.randint(lowerLimit, upperLimit)
print(numC, 'Computer guessed your number with', tries, 'tries')
+3

, ?

import random
num = int(input('Your number: '))
tries = []
numC = random.randint(1, 100)
while numC != num:
    numC = random.randint(1, 100)
    if numC in tries:
        pass
    elif numC > num:
        print(numC, 'Less')
        tries.append(numC)
    elif numC < num:
        print(numC, 'More')
        tries.append(numC)
print(numC, 'Computer guessed your number with', len(tries), 'tries')
+1

I tried, but my program can only guess if the user is telling the computer whether the computer exceeds a value higher or less than the user number. I also added all kinds of other things to make it more user friendly. It doesn't matter here:

def bp(x): if '.' in str(x): tenth=int(str(x).split('.')[1][0]) if tenth<=4: return str(x).split('.')[0] else: return int(str(x).split('.')[0])+1 else: return x guess=50 low=0 high=100 rep=0 while rep<=10: try: reply=input('Is the number %s? (Y/H/L)\n'%guess) except IndexError: continue if reply=='y': print('Haha!') break elif reply=='l': low=guess elif reply=='h': high=guess else: print("Enter 'Yes', 'Low', or 'High'") continue rep+=1 guess=bp((int(low)+int(high))/2) break

0
source

How about avoiding guessing a number in a certain range. Just use binary search , as comments report and begin with guessing 50.

import random

num = int(input('Your number: '))
tries = 1
lowerLimit = 1
upperLimit = 100
numC = 50

while numC != num:
    if numC > num:
        print(numC, 'Less')
        upperLimit = numC
    else:
        print(numC, 'More')
        lowerLimit = numC
    tries += 1
    numC = lowerLimit + (upperLimit - lowerLimit) / 2

print(numC, 'Computer guessed your number with', tries, 'tries')

You can expect the worst execution time of 7 attempts.

runtime

0
source

Source: https://habr.com/ru/post/1607806/


All Articles