python实现的简单猜数字游戏

时间:2022-09-13 22:57:09

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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程序设计有所帮助。