Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 一个关于python游戏的问题猜猜数字_Python 3.x - Fatal编程技术网

Python 3.x 一个关于python游戏的问题猜猜数字

Python 3.x 一个关于python游戏的问题猜猜数字,python-3.x,Python 3.x,我是Python的初学者,我想写一个数字猜测程序,让用户想到一个计算机必须猜测的数字。 我写了这篇文章,它是有效的,但有一个问题: 说我的秘密号码是13。计算机说45,我输入“h”,然后它说一个在(0,45)范围内的数字,例如,它说10,在我输入“l”后,我不希望它说一个超过45的数字(我说的数字太高了!),反之亦然 import random print('Please think of a number between 0 and 100!') input('''Press Enter on

我是Python的初学者,我想写一个数字猜测程序,让用户想到一个计算机必须猜测的数字。 我写了这篇文章,它是有效的,但有一个问题: 说我的秘密号码是13。计算机说45,我输入“h”,然后它说一个在(0,45)范围内的数字,例如,它说10,在我输入“l”后,我不希望它说一个超过45的数字(我说的数字太高了!),反之亦然

import random
print('Please think of a number between 0 and 100!')
input('''Press Enter once you've thought of one...''')
guess = random.randint(0,100)
print('Is your secret number', guess, '?'
  '''\nEnter 'h' to indicate the guess is too high'''
  '''\nEnter 'l' to indicate the guess is too low'''
  '''\nEnter 'c' to indicate the guess is correct''')
hlc = input()
while hlc <= 'h' or 'l' or 'c':
    if hlc == 'h':
        guess -= 1
        guess = random.randint(0, guess)
        print('Is your secret number', guess,'?')
        hlc = input()
    elif hlc == 'l':
        guess += 1
        guess = random.randint(guess, 100)
        print('Is your secret number', guess,'?')
        hlc = input()
    elif hlc == 'c':
        print('Game over. Your secret number was', guess, '!')
        break
随机导入
打印('请考虑一个介于0和100之间的数字!')
输入(''一旦想到一个…''就按Enter键')
guess=random.randint(0100)
打印('是你的秘密号码',猜猜,'?'
''\n输入'h'表示猜测过高''
''\n输入'l'表示猜测值太低''
''\n输入'c'表示猜测是正确的'')
hlc=输入()

而hlc则应追踪用户迄今为止拒绝的最高和最低数字。如下所示:

lowest_guess = 100
highest_guess = 100
while ... :
    guess = random.randint(lowest_guess, highest_guess)
    ...
    if hlc == 'h':
        highest_guess = guess
    elif hlc == 'l':
        lowest_guess = guess

这可能没有达到您的期望:
虽然hlc谢谢,但我认为这更好,但并不是我所期望的want@cricket_007你说得对。但它回答了一个提出得很好的问题,没有剥夺任何其他学习机会。