python实现的简单猜数字游戏

444次阅读  |  发布于5年以前

本文实例讲述了python实现的简单猜数字游戏。分享给大家供大家参考。具体如下:

给定一个1-99之间的数,让用户猜数字,当用户猜错时会提示用户猜的数字是过大还是过小,知道用户猜对数字为止,猜对数字用的次数越少成绩越好。


    import random
    n = random.randint(1, 99)
    guess = int(raw_input("Enter an integer from 1 to 99: "))
    while n != "guess":
      print
      if guess < n:
        print "guess is low"
        guess = int(raw_input("Enter an integer from 1 to 99: "))
      elif guess > n:
        print "guess is high"
        guess = int(raw_input("Enter an integer from 1 to 99: "))
      else:
        print "you guessed it!"
        break
      print

希望本文所述对大家的Python程序设计有所帮助。

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8