数字猜测程序故障

时间:2011-07-22 19:46:13

标签: python

  

可能重复:
  Learning Python: If condition executing all the time

这个Python代码用于猜测1到100之间的数字。'guesser()'函数没有任何问题,我知道因为我的程序也有一个完美的“手动模式”。出于某种原因,它总是将“猜测”值分配给“低”,并一直这样做,直到它等于100和程序“Ragequits”。它永远不会对“高”做任何事情,它在整个执行过程中保持默认状态。无论如何,它永远不会执行“获胜”区块。 注意:变量'high'和'low'分别从101和0开始,'guesser()'在它们之间选择一个数字。代码中较早使用'goOn'来确定此人是否想再玩一次。

            num = raw_input('Enter the number which you want the computer to guess. ')
            unguessed = True
            while unguessed:
                if high == low + 1 or high <= low:
                    print 'Waitafligginflagginminnit! You CHEATED! *Ragequits*'
                    goOn = False
                    unguessed = False
                    print ''
                    raw_input('Press Enter to continue.')
                guesses = guesses + 1
                guesss = guesser()
                print 'I guessed', guesss
                if guesss == num:
                    print 'Yay! I won in', guesses, 'guesses!'
                    again = raw_input('Just press enter if you want to play again. Otherwise...you know the drill. ')
                    unguessed = False
                    print '\n\n'
                    if again:
                        goOn = False
                else:
                    print 'Awww...'
                    if guesss > num:
                        high = guesss
                    elif guesss < num:
                        low = guesss

1 个答案:

答案 0 :(得分:5)

您应该使用int(raw_input())代替raw_input,以便将猜测保留为整数。 raw_input()单独返回一个字符串,因此比较不会成立。

请参阅之前已解答过的question,以及与此类似的question

相关问题