带有线索的Python猜谜游戏

时间:2019-07-15 18:34:51

标签: python

此处是Python(3.7)初学者。这个猜谜游戏提供了线索:“冷”,“暖”和“热”取决于玩家与游戏者的接近程度。

问题:如何添加额外的2条线索:如果下一个猜测更接近答案,则“温暖”;如果距离更远,则为“冷”。

    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.")

例如:

玩家猜测= 10,所需结果为“冷”。剩余的猜测4'

下一个猜测= 15,期望的结果为“暖身。剩余的猜测3'

下一个猜测= 12,期望的结果为“冷”。剩余的猜测2'

下一个猜测= 36,预期结果为“热门”。剩余的猜测1'

尽管36比12更符合“温暖”标准,但它也超过了“热门”门槛,因此我希望结局具有“热门”线索。

2 个答案:

答案 0 :(得分:1)

我将num而不是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.")

据我了解,上述程序会生成一个随机数,您需要猜测该数字,

  • 如果您猜出的数字小于或等于5个数字,并且接近该随机数,它将告诉您hot
  • 如果它大于5但小于20,它将告诉您warm
  • 大于20会给您cold

希望这对您有帮助!!

答案 1 :(得分:0)

这是我最接近您要求的内容。我认为您对原始问题的评论值得一读,但我认为这完全符合您在问题中的要求。

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.")

希望这会有所帮助