python 猜数字小游戏

要求:

  1、随机产生一个随机的1,100之间的数字

  2、输入大了或者小了提示并重新输入

  3、循环猜6次游戏结束

Readme:

  1、会使用random.randing方法产生一个随机数字

  2、用户输入后使用if-elif-else的方法进行判断,输入的次数使用for循环定义

  3、如果输入大于或者小于随机的数字,提示用户并且重新输入

  4、每次的输入for都会记录输入6次直接退出,并且会打印正确的数字

 流程图:

 

 

代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2016/11/29 13:43
# @Author  : Brian
# @Site    : 
# @File    : num.py
# @Software: PyCharm
import random,os                        #导入random和os的模块
os.system('cls')                        #使用os模块的system方法传入cls参数表示清理屏幕
#os.system('clear')                     #linux中的方法
secret = random.randint(1,100)          #调用random模块的.randint方法(作用:随机产生一个1,100之间的数字)
print("Please guess a number")          #打印提示语
print("This number is (1,100) a figure between")
for i in range(6):                      #使用for循环,确定循环次数
    guess = int(input("Please enter number you want:"))     #交互式用户输入
    if guess > secret:                  #if判断如果输入的大于随机的
        print("And the ideal figure a big") #打印
    elif guess < secret:                #elif判断如果输入的小于随机的
        print("And the ideal figure a Small")   #打印
    else:                               #不大于不小于就是等于
        print("Congratulations to you! Your  guess is right")   #打印
        break                           #跳出循环
else:                                   #输入的次数超过6次
    print("Game Over")                  #打印游戏结束
    print(secret)                       #打印出随机的数字
View Code

 

posted @ 2017-01-13 15:36  Brian_Zhu  阅读(736)  评论(0)    收藏  举报