当前位置:首页 » 编程语言 » python猜数字游戏代码

python猜数字游戏代码

发布时间: 2024-03-16 01:02:17

‘壹’ 用python写猜数字小游戏

核心代码给你,具体的功能还需要自己完善。

importtime,random
classGuessNum:
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))
foriinrange(0,4):
a=str(random.choice(seq_zton))#选出一个数字
self._num+=a
seq_zton.remove(int(a))#注意a的类型

self.sec=time.clock()#开始计时

defcheck_answer(self):
returnself._num

defcheck_input(self):
num_pos,num_value=0,0#位置对和数值对的分别的个数
tmp=input("Pleaseinputthenumberyouguess(Norepetition),or'c'tochecktheanswer:")
iftmp=='c':
print(self.check_answer())
tof=self.check_input()
returntof
elifnottmp.isalnumornotlen(tmp)==4:
print("Wrongformat!")
tof=self.check_input()#需要优化
returntof
self.input_num=list(tmp)
lst_temp=list(self._num)
ifself.input_num==lst_temp:#猜对
self.prt_vic()
returnTrue
foriinlst_temp:
ifiinself.input_num:
iflst_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
returnFalse

defprt_state(self,num_pos,num_value):
print("You'vegot%%dnumberswiththerightvalueonly"%(num_pos,num_value))

defprt_vic(self):
t=time.clock()
self.sec=t-self.sec
print("Congratulations!!")
print("%dtimesand%."%(self.count,self.sec))

gn=GuessNum()
whileTrue:
ss=gn.check_input()
ifss:
b=input("Continue?y/n:")
ifb=='n':
break
else:
gn=GuessNum()
continue
热点内容
zend在线加密 发布:2025-09-10 22:26:52 浏览:890
电脑改远程打印服务器 发布:2025-09-10 22:15:01 浏览:914
codeblock编译运行按钮 发布:2025-09-10 22:14:55 浏览:542
ftp网页访问 发布:2025-09-10 21:51:21 浏览:252
数据库修改视图 发布:2025-09-10 21:38:32 浏览:680
金铲铲ios安卓怎么加好友 发布:2025-09-10 21:23:03 浏览:446
ftp好处 发布:2025-09-10 21:03:44 浏览:161
s3云服务器1m买完还要付费吗 发布:2025-09-10 20:47:18 浏览:821
天下三如何配置不卡 发布:2025-09-10 20:41:35 浏览:132
按键精灵加血脚本 发布:2025-09-10 20:32:52 浏览:131