python实现猜数字游戏(无重复数字)示例分享
发布时间:2023-12-15 19:48:46
import time, random
class GuessNum:
def __init__(self):
self._num = ''
self.input_num = []
self.count = 1 #猜对所用次数
self.sec = 0 #猜对所用时间
self._generate_num()
def _generate_num(self): #产生不重复的四个数字
seq_zton = list(range(10))
for i in range(0, 4):
a = str(random.choice(seq_zton)) #选出一个数字
self._num += a
seq_zton.remove(int(a)) #注意a的类型
self.sec = time.clock() #开始计时
def check_answer(self):
return self._num
def check_input(self):
num_pos, num_value = 0, 0 #位置对和数值对的分别的个数
tmp = input("Please input the number you guess(No repetition),or 'c' to check the answer:")
if tmp == 'c':
print(self.check_answer())
tof = self.check_input()
return tof
elif not tmp.isalnum or not len(tmp) == 4:
print("Wrong format!")
tof = self.check_input() #需要优化
return tof
self.input_num = list(tmp)
lst_temp = list(self._num)
if self.input_num == lst_temp: #猜对
self.prt_vic()
return True
for i in lst_temp:
if i in self.input_num:
if lst_temp.index(i) == self.input_num.index(i): #位置也相同
num_pos += 1
num_value += 1
else:
num_value += 1
self.prt_state(num_pos, num_value)
self.count += 1
return False
def prt_state(self, num_pos, num_value):
print("You've got %d numbers with the right position and %d numbers with the right value only" % (num_pos, num_value))
def prt_vic(self):
t = time.clock()
self.sec = t - self.sec
print("Congratulations!You have successfully got the right number!")
print("%d times and %.2f sec in total to get the right answer" % (self.count, self.sec))
gn = GuessNum()
while True:
ss = gn.check_input()
if ss:
b = input("Continue? y/n:")
if b == 'n':
break
else:
gn = GuessNum()
continue


猜你喜欢
- 广播的原则如果两个数组的后缘维度(从末尾开始算起的维度)的轴长度相符或其中一方的长度为1,则认为它们是广播兼容的。广播会在缺失维度和(或)轴
- vue组件在prop里根据type决定传值还是传引用。简要如下:传值:String、Number、Boolean传引用:Array、Obje
- 下面是BeforeInitialBind事件过程:<SCRIPT language=vbscript event=
- 原型扩展:>> String.prototype :String对象原型扩展 --------------
- 数据库:保存图片的数据格式 图象二进制数据储存字段前台: <%@ Page Language="C#" AutoE
- 1. 视图分离与嵌套在 learnlaravel 文件夹下运行命令:php artisan generate:view admin._lay
- 表级锁该锁会锁定整张表,它是MySQL中最基本的锁策略,并不依赖于存储引擎(不管你是MySQL的什么存储引擎,对于表锁的策略都是一样的),并
- 在matplotlib中,imshow方法用于绘制热图,基本用法如下import matplotlib.pyplot as pltimpor
- PyQt5+requests实现一个车票查询工具,供大家参考,具体内容如下结构图效果图思路1、search(QPushButton)点击信号
- 一、概述面向对象编程 (OOP) 语言的一个主要功能就是“继承”。继承是指这样一种能力:它可以使用现有类的所有功能,并在无需重新编写原来的类
- 先看看单条 SQL 语句的分页 SQL 吧。 方法1: 适用于 SQL Server 2000/2005 SELECT TOP 页大小 *
- 一 概念介绍Index Condition Pushdown (ICP)是MySQL 5.6 版本中的新特性,是一种在存储引擎层使用索引过滤
- 本文实例讲述了python类装饰器用法。分享给大家供大家参考。具体如下:#!coding=utf-8 registry = {} def r
- 由于要学习搭建服务器和数据库,所以最近开始自学sql语言了,至于写数据库就用比较基础的Mysql数据库了,虽然Mysql已经被互联网公司所淘
- 注:IE8以前的版本均不支持该特性为了向文档中插入生成内容,可以使用:before与:after伪元素。如,我想在所有链接的后面加上&quo
- 优化前后新老代码如下:from git_tools.git_tool import get_collect_projects, QQNews
- 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用。但是如果离
- 函数:endswith()作用:判断字符串是否以指定字符或子字符串结尾,常用于判断文件类型相关函数:判断字符串开头 startswith()
- event.keycode值大全 1 keycode 8 = BackSpace BackSpace 2 keycode 9 = Tab T
- 前言本文介绍的主要内容是 Redux-Toolkit 在 React + TypeScript 大型应用中的实践,主要解决的问题是使用 cr