python实现猜数字小游戏

作者:雨者 时间:2021-09-26 02:23:11 

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经典小游戏汇总

python微信跳一跳游戏集合

来源:https://blog.csdn.net/peterz1997/article/details/76862289

标签:python,猜数字,游戏
0
投稿

猜你喜欢

  • Python matplotlib生成图片背景透明的示例代码

    2022-07-04 06:22:57
  • Python语法中的模糊语义

    2022-09-13 01:22:57
  • Python如何查看并打印matplotlib中所有的colormap(cmap)类型

    2023-12-05 11:04:28
  • pandas按某列降序的实现

    2021-02-19 08:29:44
  • python处理按钮消息的实例详解

    2023-04-23 03:59:25
  • 增强网站的魅力 网页制作技巧三则

    2007-10-04 10:06:00
  • Python selenium 三种等待方式解读

    2023-12-26 05:37:56
  • Python使用graphviz画流程图过程解析

    2022-06-19 06:45:18
  • Python内置函数bin() oct()等实现进制转换

    2021-05-15 05:15:42
  • python中sys.argv参数用法实例分析

    2021-09-19 20:29:30
  • python optparse模块使用实例

    2021-02-03 22:01:36
  • Python实现HTTP协议下的文件下载方法总结

    2023-04-29 20:01:36
  • Python中psutil模块使用汇总

    2022-07-30 10:26:45
  • 日期垂直排列的两种技巧

    2009-08-28 12:38:00
  • Python socket模块ftp传输文件过程解析

    2021-04-17 02:22:59
  • Python利用三层神经网络实现手写数字分类详解

    2023-10-06 19:53:40
  • Python+Opencv实现数字识别的示例代码

    2021-03-10 14:16:38
  • js截取字符串的两种方法及区别详解

    2013-08-31 04:16:37
  • Python httplib,smtplib使用方法

    2021-12-23 04:34:29
  • Dreamweaver技巧十二招

    2009-07-05 18:53:00
  • asp之家 网络编程 m.aspxhome.com