电脑开始玩猜谜游戏

时间:2015-04-08 16:49:26

标签: python

游戏的目的是猜测数字,但角色是反向的。我猜数字和计算机选择正确的数字。我完成了清单并且它有效,但我需要一个部分的帮助。如果猜测太高或太低,如何在输入数字后添加选项?如果com说5,那么我可以说“它太低或太高”。提前谢谢。

    from random import randint
    def computer_guess(num):
        low = 1
        high = 100

        guess = randint(1,100)
        while guess != num:
           print("The computer takes a guess...", guess)
           if guess > num:
              high = guess
           elif guess < num:
               low = guess + 1

           guess = (low+high)//2    

       print("The computron guessed", guess, "and it was correct!")
       print(" I computron won the battle!")

 def main():
       print("I am computron, I accept your guessing game!")
       num = int(input("\n\nChoose a number for the computer to guess: "))
       if num < 1 or num > 100:
           print("Must be in range [1, 100]")
       else:
           computer_guess(num)

if __name__ == '__main__':
main()

1 个答案:

答案 0 :(得分:0)

from random import randint

def computer_guess(num):
    low = 1
    high = 100

    guess = randint(1,100)
    while guess != num:
        print("The computer takes a guess...", guess)
        if guess > num:
            high = guess
            print ' Computron guessed higher'
        elif guess < num:
            low = guess + 1
            print 'Computron guessed lower'

        guess = (low+high)//2    

    print("The computron guessed", guess, "and it was correct!")
    print(" I computron won the battle!")

def main():
    print("I am computron, I accept your guessing game!")
    num = int(input("\n\nChoose a number for the computer to guess: "))
    if num < 1 or num > 100:
        print("Must be in range [1, 100]")
    else:
        computer_guess(num)

if __name__ == '__main__':
    main()
    while True:
        ask = raw_input('Do you want to play again [y/n]:')
        if ask.lower() =='y':
            main()
        else:
            print 'bye'
            break

#Output

I am computron, I accept your guessing game!


Choose a number for the computer to guess: 56
('The computer takes a guess...', 25)
Computron guessed lower
('The computer takes a guess...', 63)
 Computron guessed higher
('The computer takes a guess...', 44)
Computron guessed lower
('The computer takes a guess...', 54)
Computron guessed lower
('The computer takes a guess...', 59)
 Computron guessed higher
('The computer takes a guess...', 57)
 Computron guessed higher
('The computron guessed', 56, 'and it was correct!')
 I computron won the battle!
Do you want to play again [y/n]:y
I am computron, I accept your guessing game!


Choose a number for the computer to guess: 44
('The computer takes a guess...', 40)
Computron guessed lower
('The computer takes a guess...', 70)
 Computron guessed higher
('The computer takes a guess...', 55)
 Computron guessed higher
('The computer takes a guess...', 48)
 Computron guessed higher
('The computron guessed', 44, 'and it was correct!')
 I computron won the battle!
Do you want to play again [y/n]:n
bye