Python实现猜数字小游戏

yipeiwu_com7年前Python基础

Python初学者小游戏:猜数字

游戏逻辑:电脑随机生成一个数字,然后玩家猜数字,电脑提示猜的数字大了还是小了,供玩家缩小数字范围,达到既定次数后,玩家失败。若在次数内猜对,玩家获胜。

涉及知识点:random.randint() , print() , input() ( raw_input() )

参考实现代码:

#!/usr/bin/env python 
# encoding: utf-8 
 
#使用print("",end=...)标准 
from __future__ import print_function 
 
import os 
import sys 
import time 
import random 
 
#输入检测 
 
while 1: 
  os.system('cls') 
  print ("Hello , Welcome to Guess_Number Games...The Number is between 1 - 10...") 
  print ("Please input the level you want(1~10): ",end = '') 
  level = raw_input("") 
  diff = 11-int(level) 
  if diff > 10 or diff <1: 
    print ("Invalid Input...") 
    time.sleep(0.3) 
  else: 
    break 
 
#猜数字流程 
 
count_num = 0 
ran = random.randint(1,10) 
while count_num < diff: 
  count_num += 1 
  print (str(count_num)+": "+"Please input the number you guess: ",end = '') 
  number = raw_input() 
  number = int(number) 
  if number < ran: 
    print ("Too Little...") 
    continue 
  elif number > ran: 
    print ("Too Big...") 
    continue 
  else: 
    print ("Congraduation! You Win...") 
    break 
if count_num == diff: 
  print ("You Lose...") 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python的一些用法分享

1)正则表达式的使用。 复制代码 代码如下: #正则表达式的模块 import re #正则表达式 rePattern = '.*[0-9]{4}' pattern = re.compi...

python中django框架通过正则搜索页面上email地址的方法

本文实例讲述了python中django框架通过正则搜索页面上email地址的方法。分享给大家供大家参考。具体实现方法如下: import re from django.shortc...

详解Python logging调用Logger.info方法的处理过程

详解Python logging调用Logger.info方法的处理过程

本次分析一下Logger.info的流程 1. Logger.info源码: def info(self, msg, *args, **kwargs): """ Log...

Python中返回字典键的值的values()方法使用

 values()方法返回给定的字典中所有可用值的列表。 语法 以下是values()方法的语法: dict.values() 参数   &...

Python实现扩展内置类型的方法分析

本文实例讲述了Python实现扩展内置类型的方法。分享给大家供大家参考,具体如下: 简介 除了实现新的类型的对象方式外,有时我们也可以通过扩展Python内置类型,从而支持其它类型的数据...