python初学者的自我修养
这个寒假主要的成绩是自学了一点点python。然后嘛,根据剧本,不管我新学什么语言我都要用他来编一个小游戏。。。
#!/usr/bin/python #coding=UTF-8 maxium=100 maxtime=8 #this function returns a number input from user,and the num ranges from nowmin to nowmax def inp_num(s='input a num:',nowmin=1,nowmax=maxium): num=int(raw_input(s)) while num not in range(nowmin,nowmax): num=int(raw_input('''its not a num from '''+str(nowmin)+'''~'''+str(nowmax)+''' please input again:''')) return num #this function returns a string filled with blank def longspace(length): return "".zfill(length).replace("0"," ") #this function prints a graph,whose width is wid #and the graph depends on lst,each element makes a line #the blank string element makes a blank line #a list element makes some words,the first element of the list is the content #and the second element is the showing style,c means center,l means left and r means right def graph(wid,lst): maxstr='*' empty='*' for tmp in range(0,wid): maxstr+='*' empty+=' ' maxstr+='*' empty+='*' print maxstr for tmp in lst: if tmp=='': print empty else: if len(tmp)==1 or tmp[1]=='c': print '*'+tmp[0].center(wid)+'*' elif tmp[1]=='l': print '*'+tmp[0]+longspace(wid-len(tmp[0]))+'*' elif tmp[1]=='r': print '*'+longspace(wid-len(tmp[0]))+tmp[0]+'*' print maxstr #the main procedure of the guessing game def game(): print 'Game Start' tar=random.choice(rg) nowmin=1 nowmax=maxium time=maxtime temp=inp_num("input your first guess,range from "+str(nowmin)+" to "+str(nowmax)+":") while time>0 and temp!=tar: if temp>tar: print "Too big,"+str(time)+" times left" nowmax=temp else: print "Too small,"+str(time)+" times left" nowmin=temp time=time-1 temp=inp_num("input your next guess,range from "+str(nowmin)+" to "+str(nowmax)+":",nowmin,nowmax) else: if temp==tar: graph(25,['',['You Win'],['In '+str(time)+' Times'],'']) else: graph(25,['',['You Lose'],['Answer is '+str(tar)],'']) #the main part of this program import random rg=range(1,maxium) graph(30,['','',['Guessing Game','c'],['by Octopus ','r'],'','']) s="y" while s.lower()=="y": game() s=raw_input("wanna play again?[y/n]:")
2016
02 24
上一篇
Older
下一篇
Newer
评论
0
点击
1923