带线索的Python猜谜游戏
这里是Python(3.7)的初学者。此猜谜游戏提供范围线索:带线索的Python猜谜游戏,python,Python,这里是Python(3.7)的初学者。此猜谜游戏提供范围线索: Cold Warm Hot 取决于玩家离回答的距离 问题:如何添加额外的3条增量线索: Colder 加热器 更热 如果下一个猜测距离答案更远,则使用Colder。 如果下一个猜测更接近答案,则使用warter。 如果其在热范围内,则使用热代替热 第一次猜测产生范围线索冷、热或热 随后的猜测将产生越来越多的线索冷的或热的/热的,如果,而它们落在与先前猜测相同的范围内 如果它们不在该范围内,则在该范围内,将首先生成范围线
Cold
Warm
Hot
Colder
加热器
如果下一个猜测距离答案更远,则使用更热
。Colder
如果下一个猜测更接近答案,则使用
。warter
如果其在
范围内,则使用热
代替热
热
冷
、热
或热
随后的猜测将产生越来越多的线索冷的
或热的
/热的
,如果,而它们落在与先前猜测相同的范围内
如果它们不在该范围内,则在该范围内,将首先生成范围线索冷
、暖
或热
,然后生成冷
或热
。换句话说,冷
,热
或热
范围线索的优先级高于增量冷
或热
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.")
print(“密码在0到100之间,你可以猜5次。”)
user_input=int(输入('Make a guess'))
计数=0
当用户输入不是41且计数小于4时:
计数=计数+1
how_close_to_answer=41-用户输入
如果5=20:
user_input=int(输入(f'Cold.剩余猜测{5-count}'))
其他:
user_input=int(输入(f'Hot.剩余猜测{5-count}'))
如果用户输入不是41:
打印(“你输了!”)
其他:
打印(“你赢了!”)
print(f“你花了{count+1}次猜测才得到正确答案。”)
例如(在无限猜测n的情况下):
Cold
。剩余猜测(n-1)'warter
。剩余猜测(n-2)'更冷
。剩余猜测(n-3)'Hot
。剩余猜测(n-4)'更热
。剩余猜测(n-5)'Warm
。剩余猜测(n-6)'在6。示例-数字30比之前的37更冷,但它也属于
Warm
范围,因此给出了Warm
线索。我将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.")
随机导入
打印(“密码在0到100之间。你有5次猜测。”)
user_input=int(输入('Make a guess'))
计数=0
num=random.randint(1101)
当用户输入不是num且计数小于4时:
#取消注释下面的行以查看随机生成的数字
#打印('生成的随机数='+str(num))
计数=计数+1
how_close_to_answer=num-用户输入
如果abs(how_close_to_answer)>5且abs(how_close_to_answer)=20:
user_input=int(输入(f'Cold.剩余猜测{5-count}'))
其他:
user_input=int(输入(f'Hot.剩余猜测{5-count}'))
如果用户输入不是num:
打印(“你输了!”)
其他:
打印(“你赢了!”)
print(f“你花了{count+1}次猜测才得到正确答案。”)
据我所知,上面的程序会生成一个随机数,你需要猜测这个数
- 如果您猜测的数字小于或等于接近该随机数字的
位,它将告诉您5
hot
- 如果它大于
且小于5
,则它将告诉您20
warm
- 在大于
将给您20时
cold
希望这对你有帮助 这是我最接近你要求的。在我看来,关于你最初问题的评论值得一读,但我认为这正是你在问题中要求的
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.")
print(“密码在0到100之间,你可以猜5次。”)
user_input=int(输入('Make a guess'))
计数=0
最后距离=-1
当用户输入不是41且计数小于4时:
计数=计数+1
how_close_to_answer=(41-用户输入)
how_close_to_answer=how_close_to_answer.\uuu abs_uuuuuuuuu()
如果您如何接近答案5:
user_input=int(输入(f'Hot.剩余猜测{5-count}'))
elif last_距离==-1:
如果5=20:
user_input=int(输入(f'Cold.剩余猜测{5-count}'))
艾利夫:第一件事就是要准确地决定这些额外的线索是如何发挥作用的。如果非要我猜的话,你希望在第一次猜测之后,所有的猜测都是说更热
,更冷
,或者热
。如果玩家猜到一个HOT
猜测,然后得到HOT
范围内的下一个猜测,那么你的代码应该再次说HOT
,还是说warter
/colder
?除了@Hoog建议之外,你可能还想根据他们的猜测是接近还是远离答案来做。i、 然后猜35,然后40,“更热”,然后38,“更冷”。这样你是说如果他们走了,他们会变得“更暖和”