导航:首页 > 编程语言 > python编写多行代码

python编写多行代码

发布时间:2023-12-19 05:36:13

1. 求python大神给个200行左右的代码,多点注释,做期末大作业

#-*-coding:utf-8-*-

importcurses#引入curses模块,curses是一个在Linux/Unix下广泛应用的图形函数库.,作用是可以绘制在DOS下的用户界面和漂亮的图形。
fromrandomimportrandrange,choice#从random模块引入randrange,choice这两个类
#从collections引入defaultdict这个类

letter_codes=[ord(ch)forchin'WASDRQwasdrq']#ord函数是把字符转换成对应的数字
actions=['Up','Left','Down','Right','Restart','Exit']#上,左,下,右,重启,退出
actions_dict=dict(zip(letter_codes,actions*2))#把字母与动作对应起来。zip是把元组中的值对应起来。

#############################
WUp
ALeft
SDown
DRight
RRestart
QExit

wUp
aLeft
sDown
dRight
rRestart
QExit
##############################################
defget_user_action(keyboard):
char="N"#char的初始值为Nwhilecharnotinactions_dict:
char=keyboard.getch()returnactions_dict[char]#阻塞+循环,直到获得用户有效输入才返回对应行为deftranspose(field):return[list(row)forrowinzip(*field)]#zip函数里边加*号,是把行变列,列变行。所以这句代码是行列转置definvert(field):return[row[::-1]forrowinfield]#这句代码是把列表前后颠倒classGameField(object):#创建一个叫做GameField的类,用来创建棋盘def__init__(self,height=4,width=4,win=2048):这个类三个参数self.height=height#高self.width=width#宽self.win_value=win#过关分数self.score=0#当前分数self.highscore=0#最高分self.reset()#重置棋盘defreset(self):#定义一个reset函数ifself.score>self.highscore:#如果当前分数大于最高分,那么把当前分数赋值给最高分self.highscore=self.scoreself.score=0#当前分数恢复到0分self.field=[[0foriinrange(self.width)]forjinrange(self.height)]#横纵坐标恢复到(0,0)self.spawn()#调用spawn这个函数self.spawn()defmove(self,direction):#定义move函数defmove_row_left(row):#向左移deftighten(row):#squeesenon-zeroelementstogether把零散的非零单元挤到一块
new_row=[iforiinrowifi!=0]#如果i不等于零,把他们赋值到new_row这个元组中
new_row+=[0foriinrange(len(row)-len(new_row))]#其余位置用0补充returnnew_row#返回这个元组defmerge(row):#定义merge函数,用来合并单元
pair=False#pair初始值为假
new_row=[]#new_row初始值为空foriinrange(len(row)):#让i在格子里循环ifpair:如果pair为真
new_row.append(2*row[i])#那么把把row【i】的值乘以2,追加到new_row后边self.score+=2*row[i]#并且得分为row【i】的值乘以2
pair=False#pair重新赋值为假else:如果pair为真ifi+1<len(row)androw[i]==row[i+1]:#如果i+1还没到边界,并且此时的row【i】=row【i+1】
pair=True#那么pair为真
new_row.append(0)#new_row后追加零else:
new_row.append(row[i])#否则追加row【i】
assertlen(new_row)==len(row)#提醒两者长度一致returnnew_rowreturntighten(merge(tighten(row)))#反复合并,知道不能合并为止

moves={}
moves['Left']=lambdafield:
[move_row_left(row)forrowinfield]#做移动
moves['Right']=lambdafield:
invert(moves['Left'](invert(field)))#invert是逆转
moves['Up']=lambdafield:
transpose(moves['Left'](transpose(field)))#transpose是转置
moves['Down']=lambdafield:
transpose(moves['Right'](transpose(field)))ifdirectioninmoves:ifself.move_is_possible(direction):#如果移动方向在四个方向上,self.field=moves[direction](self.field)那么调用moves函数self.spawn()#产生随机数returnTrueelse:returnFalsedefis_win(self):returnany(any(i>=self.win_valueforiinrow)forrowinself.field)defis_gameover(self):returnnotany(self.move_is_possible(move)formoveinactions)defdraw(self,screen):
help_string1='(W)Up(S)Down(A)Left(D)Right'
help_string2='(R)Restart(Q)Exit'
gameover_string='GAMEOVER'
win_string='YOUWIN!'defcast(string):
screen.addstr(string+' ')defdraw_hor_separator():
line='+'+('+------'*self.width+'+')[1:]
separator=defaultdict(lambda:line)ifnothasattr(draw_hor_separator,"counter"):
draw_hor_separator.counter=0
cast(separator[draw_hor_separator.counter])
draw_hor_separator.counter+=1defdraw_row(row):
cast(''.join('|{:^5}'.format(num)ifnum>0else'|'fornuminrow)+'|')

screen.clear()
cast('SCORE:'+str(self.score))if0!=self.highscore:
cast('HGHSCORE:'+str(self.highscore))forrowinself.field:
draw_hor_separator()
draw_row(row)
draw_hor_separator()ifself.is_win():
cast(win_string)else:ifself.is_gameover():
cast(gameover_string)else:
cast(help_string1)
cast(help_string2)defspawn(self):
new_element=4ifrandrange(100)>89else2
(i,j)=choice([(i,j)foriinrange(self.width)forjinrange(self.height)ifself.field[i][j]==0])self.field[i][j]=new_elementdefmove_is_possible(self,direction):defrow_is_left_movable(row):
defchange(i):#trueifthere'llbechangeini-thtileifrow[i]==0androw[i+1]!=0:#MovereturnTrueifrow[i]!=0androw[i+1]==row[i]:#(change(i)foriinrange(len(row)-1))

check={}
check['Left']=lambdafield:
any(row_is_left_movable(row)forrowinfield)

check['Right']=lambdafield:
check['Left'](invert(field))

check['Up']=lambdafield:
check['Left'](transpose(field))

check['Down']=lambdafield:
check['Right'](transpose(field))ifdirectionincheck:returncheck[direction](self.field)else:returnFalsedefmain(stdscr):definit():#重置游戏棋盘
game_field.reset()return'Game'defnot_game(state):#画出GameOver或者Win的界面
game_field.draw(stdscr)#读取用户输入得到action,判断是重启游戏还是结束游戏
action=get_user_action(stdscr)
responses=defaultdict(lambda:state)#默认是当前状态,没有行为就会一直在当前界面循环
responses['Restart'],responses['Exit']='Init','Exit'#对应不同的行为转换到不同的状态returnresponses[action]defgame():#画出当前棋盘状态
game_field.draw(stdscr)#读取用户输入得到action
action=get_user_action(stdscr)ifaction=='Restart':return'Init'ifaction=='Exit':return'Exit'ifgame_field.move(action):#movesuccessfulifgame_field.is_win():return'Win'ifgame_field.is_gameover():return'Gameover'return'Game'


state_actions={'Init':init,'Win':lambda:not_game('Win'),'Gameover':lambda:not_game('Gameover'),'Game':game
}

curses.use_default_colors()
game_field=GameField(win=32)


state='Init'#状态机开始循环whilestate!='Exit':
state=state_actions[state]()

curses.wrapper(main)

2. 写一个20行以上的python简单代码

classPrice:
ticket_d=100
ticket_w=ticket_d*1.2
defrq(self):
self.a=int(input('请输入是平日还是周末(平日:1/周末:0):'))
ifself.a==0:
self.p=self.ticket_w
ifself.a==1:
self.p=self.ticket_d
defpj(self):
input('请输入是人数'+' ')
self.ad=int(input('大人数量:'))
self.ch=int(input('儿童数量:'))
self.money=self.p*self.ad+self.p*self.ch/2
print('%f'%self.money)

classTicket():
def__init__(self,weekend=False,child=False):
self.exp=100
ifweekend:
self.inc=1.2
else:
self.inc=1
ifchild:
self.discount=0.5
else:
self.discount=1
defcalcPrice(self,num):
returnself.exp*self.inc*self.discount*num

alt=Ticket()
child=Ticket(child=True)
print("2个成人+1个小孩平日票价为:%.2f"%(alt.calcPrice(2)+child.calcPrice(1)))

阅读全文

与python编写多行代码相关的资料

热点内容
世界史纲pdf 浏览:129
湖北社保年审app叫什么名字 浏览:848
迈达克云服务器 浏览:595
mfc深入浅出从mfc设计到mfc编程 浏览:81
萤石云服务器连接设置 浏览:325
中国名着pdf 浏览:590
华为服务器设备序列号怎么看 浏览:319
跑永辉生活配送用什么app 浏览:147
ug识别符号命令在哪里 浏览:719
pdf文件改文字 浏览:732
查询qq号剑灵服务器地址 浏览:552
国家反诈中心app为什么要刷脸 浏览:303
iphone怎么修改dns服务器地址 浏览:85
bandizip解压位置 浏览:168
服务器的防火墙如何访问 浏览:306
javagoto关键字 浏览:847
广州少儿编程加盟排名榜 浏览:122
51单片机th0 浏览:292
冠军交易pdf 浏览:208
excelword转换成pdf 浏览:389