python反向猜谜游戏不同的打印消息

python反向猜谜游戏不同的打印消息,python,Python,当我试图打印一条不同的消息,因为有多个猜测时,我得到了一条双重打印消息,因为在一次迭代中有猜测 if guess >= 2: print ('Your number is %d. I found it in %d guesses.' % (guess, iteration)) if iteration == 1: print ('I found your number in 1 guess.') 电流输出: 输入两个数字,先低后高。 低=2 高=8 在此行的第一次循环迭代

当我试图打印一条不同的消息,因为有多个猜测时,我得到了一条双重打印消息,因为在一次迭代中有猜测

if guess >= 2:
    print ('Your number is %d. I found it in %d guesses.' % (guess, iteration))
if iteration == 1:
    print ('I found your number in 1 guess.')
电流输出: 输入两个数字,先低后高。 低=2 高=8


在此行的第一次循环迭代中,您没有定义什么是“最小否”和“最大否”:

guess = guess_number(min_no, max_no)
最后一个if是比较“猜测”数字而不是“迭代”计数器:

  • 我不知道如何让程序打印不同的消息,让程序在一次猜测中猜测数字,而不是在多次猜测中猜测数字
  • 您正在检查错误的值。更改此项:

    if guess == 1:
    
    致:

  • 当用户未能正确响应小于/大于提示时,我需要程序给我一条关于输入不一致的消息 您使用的是
    else
    ,因为
    hint.lower()
    肯定是一个有效的字母,并且您已经检查了所有可能的值。在上面添加一个
    else
    是徒劳的,不是您需要的。当
    min\u no
    大于
    max\u no
    时,检测到不一致。因此,改变:

    else:
        raise ValueError('Your answers have not been consistent.')
    
    max_no = guess
    
    为此:

    if min_no > max_no:
        raise ValueError('Your answers have not been consistent.')
    
    max_no = guess - 1
    
    min_no = guess + 1
    
    其他改进 要获取两个整数之间的随机整数,包括两个可能的结果,请不要使用
    randrange
    ,而是使用
    randint
    。否则,将永远不会选择第二个值

    您的代码有时会产生与先前猜测相同的数字,即使用户回答为
    L
    G
    。这是因为您将猜测保持在新的可能值范围内。因此,改变:

    else:
        raise ValueError('Your answers have not been consistent.')
    
    max_no = guess
    
    为此:

    if min_no > max_no:
        raise ValueError('Your answers have not been consistent.')
    
    max_no = guess - 1
    
    min_no = guess + 1
    
    而且:

    min_no = guess
    
    为此:

    if min_no > max_no:
        raise ValueError('Your answers have not been consistent.')
    
    max_no = guess - 1
    
    min_no = guess + 1
    

    感谢您的帮助,当我尝试时,ValueError消息仍然无法正确打印:回溯(最近一次呼叫上次):文件“Question.py”,第58行,在raise ValueError中(“您的答案不一致”。)ValueError:您的答案不一致。什么意思是打印不正确?这就是异常打印的方式,无论您是自己提出异常(如这里),还是由本机错误触发异常。如果您不想这样,那么为什么要使用
    raise