iguors
iguors

Reputation: 11

Python guessing game with clues

A Python(3.7) beginner here. This guessing game gives range clues:

Depending on how close to answer player is.

Problem: how to add extra 3 incremental clues:

The first guess produces the range clues Cold, Warm or Hot.

The subsequent guesses will produce incremetal clues Colder or Warmer/Hotter if while they land in same range as previous guess.

If they fall out of the range, the range clues Cold, Warm or Hot will be produced first and then Colder or Warmer/Hotter while in that range. in other words Cold, Warm or Hot range clues have higher priority than incremental Colder or Warmer/Hotter.

print("The secret number is somewhere between 0 and 100. You have 5 guesses.")
user_input = int(input('Make a guess '))

count = 0

while user_input is not 41 and count < 4:
    count = count + 1
    how_close_to_answer = 41 - user_input
    if 5 < how_close_to_answer.__abs__() < 20:
        user_input = int(input(f'Warm. Remaining guesses {5 - count} '))
    elif how_close_to_answer.__abs__() >= 20:
        user_input = int(input(f'Cold. Remaining guesses {5 - count} '))
    else:
        user_input = int(input(f'Hot. Remaining guesses {5 - count} '))


if user_input is not 41:
    print('You Lose!')
else:
    print('You Win!')
    print(f"It took you {count + 1} guesses to get this correct.")

For example (in case of infinite guesses n):

  1. player guesses = 10 , desired outcome 'Cold. Remaining guesses (n-1) '
  2. next guess = 15 , desired outcome 'Warmer. Remaining guesses (n-2) '
  3. next guess = 12 , desired outcome 'Colder. Remaining guesses (n-3) '
  4. next guess = 36 , desired outcome 'Hot. Remaining guesses (n-4) '
  5. next guess = 37 , desired outcome 'Hotter. Remaining guesses (n-5) '
  6. next guess = 30 , desired outcome 'Warm. Remaining guesses (n-6) '

In 4. example - number 36 is Warmer than previous 12, but it also falls in the Hot range so the Hot clue is given instead.

in 6. example - number 30 is Colder than previous 37, but it also falls in the Warm range so the Warm clue is given instead.

Upvotes: 0

Views: 1722

Answers (2)

pjmaracs
pjmaracs

Reputation: 118

This is the closest I could get to what you asked for. The comments on your original question are worth a read in my opinion but I think this does exactly what you asked for in the question.

print("The secret number is somewhere between 0 and 100. You have 5 guesses.")
user_input = int(input('Make a guess '))

count = 0
last_distance = -1
while user_input is not 41 and count < 4:
    count = count + 1
    how_close_to_answer = (41 - user_input)
    how_close_to_answer = how_close_to_answer.__abs__()
    if how_close_to_answer <= 5 and last_distance > 5:
        user_input = int(input(f'Hot. Remaining guesses {5 - count} '))
    elif last_distance == -1:
        if 5 < how_close_to_answer < 20:
            user_input = int(input(f'Warm. Remaining guesses {5 - count} '))
        elif how_close_to_answer >= 20:
            user_input = int(input(f'Cold. Remaining guesses {5 - count} '))
        elif how_close_to_answer <= 5:
            user_input = int(input(f'Hot. Remaining guesses {5 - count} '))
    else:
        if how_close_to_answer < last_distance:
            if how_close_to_answer <= 5:
                user_input = int(input(f'Hotter. Remaining guesses {5 - count} '))
            else:
                user_input = int(input(f'Warmer. Remaining guesses {5 - count} '))
        elif how_close_to_answer > last_distance:
            user_input = int(input(f'Colder. Remaining guesses {5 - count} '))
    last_distance = how_close_to_answer


if user_input is not 41:
    print('You Lose!')
else:
    print('You Win!')
    print(f"It took you {count + 1} guesses to get this correct.")

Hope this is what helps

Upvotes: 0

Taha
Taha

Reputation: 675

I took num as a random generated number instead of 41.

import random 

print("The secret number is somewhere between 0 and 100. You have 5 guesses.")
user_input = int(input('Make a guess '))

count = 0
num = random.randint(1,101)

while user_input is not num and count < 4:
    #uncomment the line below to see random generated number
    #print('Generated Random Number= '+str(num))
    count = count + 1
    how_close_to_answer = num - user_input

    if abs(how_close_to_answer)>5 and abs(how_close_to_answer) <20 :
        user_input = int(input(f'Warm. Remaining guesses {5 - count} '))
    elif abs(how_close_to_answer) >= 20 :
        user_input = int(input(f'Cold. Remaining guesses {5 - count} '))
    else:
        user_input = int(input(f'Hot. Remaining guesses {5 - count} '))


if user_input is not num:
    print('You Lose!')
else:
    print('You Win!')
    print(f"It took you {count + 1} guesses to get this correct.")

As far as i understood , the above program generates a random number and you need to guess that number ,

  • if your guessed number is less then or equivalent to 5 digits closer to that random number it will tell you hot
  • if its greater than 5 and less than 20 then it will tell you warm
  • on greater than 20 it will give you cold

Hope this will help you !!

Upvotes: 1

Related Questions