Home > Backend Development > Python Tutorial > python实现的简单猜数字游戏

python实现的简单猜数字游戏

WBOY
Release: 2016-06-06 11:23:29
Original
1475 people have browsed it

本文实例讲述了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
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template