温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python实现猜数字小游戏

发布时间:2020-09-24 19:31:07 来源:脚本之家 阅读:133 作者:雨者 栏目:开发技术

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

python微信跳一跳游戏集合

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI