目录
一、我们先把模块准备好。
我们要准备啥模块呢?叫“VIP_module1”。
不过……
我在网上查了,发现……
没有!
所以,先创建一个文件,叫“VIP_module1.py”。代码:
def MAXorMIN(Max_or_Min:type[str],_list:type[list])->int|None:
try:
if Max_or_Min=='Max':
return max(tuple(_list))
elif Max_or_Min=='Min':
return min(tuple(_list))
except:
return None
def str_in_list2int_in_list(_list:type[list])->list|None:
try:
New_list=[]
for i in _list:
New_list.append(int(i))
return New_list
except:
return None
def Tfote(_list:type[list],element)->int|None:
for i in range(len(_list)):
if _list[i]==element:
return i+1
break
def print_xy_list(_list:type[list])->None:
for i in range(len(_list)):
print('第%d行: '%(i+1),end='')
for j in _list[i]:
print(j,end=' ')
print('')
def _01(_int:type[int])->str|None:
if _int<10:
return '0'+str(_int)
elif 10<=_int<100:
return str(_int)
好了,要准备的模块已经准备完了,开始写代码吧!
二、代码
1.导入模块
使用以下代码即可:
import VIP_module1 as VM
2.将该准备的准备好
这里要设置几个变量:
a=[[' ',' ',' ',' '],[' ',' ',' ',' '],[' ',' ',' ',' '],[' ',' ',' ',' ']]
b=[0,0,0,0]
c=['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16']
z=0
3.最难的部分也是最复杂的部分:输入、运算和输出!
while True:
z+=1
if c==[]:
print('你胜利了,你的结果是:')
VM.print_xy_list(a)
print('你用了%d次。'%z)
break
d=VM.MAXorMIN('Min',VM.str_in_list2int_in_list(c))
VM.print_xy_list(a)
e=int(input('行数(1~4),要放"'+str(d)+'": '))
if b[e-1]==0:
a[e-1][0]=c[VM.Tfote(VM.str_in_list2int_in_list(c),d)-1]
b[e-1]+=d
c.remove(VM._01(d))
else:
if b[e-1]<d:
for i in a[e-1]:
if i!=' ':
c.append(i)
a[e-1]=[VM._01(d),' ',' ',' ']
c.remove(VM._01(d))
b[e-1]=d
elif VM.Tfote(a[e-1],' ')==None:
print('UNKNOWN ERROR: THE FORMAT IS OUT OF RANGE.')
break
else:
a[e-1][VM.Tfote(a[e-1],' ')-1]=VM._01(d)
b[e-1]+=d
c.remove(VM._01(d))
三、最终代码:
import VIP_module1 as VM
a=[[' ',' ',' ',' '],[' ',' ',' ',' '],[' ',' ',' ',' '],[' ',' ',' ',' ']]
b=[0,0,0,0]
c=['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16']
z=0
while True:
z+=1
if c==[]:
print('你胜利了,你的结果是:')
VM.print_xy_list(a)
print('你用了%d次。'%z)
break
d=VM.MAXorMIN('Min',VM.str_in_list2int_in_list(c))
VM.print_xy_list(a)
e=int(input('行数(1~4),要放"'+str(d)+'": '))
if b[e-1]==0:
a[e-1][0]=c[VM.Tfote(VM.str_in_list2int_in_list(c),d)-1]
b[e-1]+=d
c.remove(VM._01(d))
else:
if b[e-1]<d:
for i in a[e-1]:
if i!=' ':
c.append(i)
a[e-1]=[VM._01(d),' ',' ',' ']
c.remove(VM._01(d))
b[e-1]=d
elif VM.Tfote(a[e-1],' ')==None:
print('UNKNOWN ERROR: THE FORMAT IS OUT OF RANGE.')
break
else:
a[e-1][VM.Tfote(a[e-1],' ')-1]=VM._01(d)
b[e-1]+=d
c.remove(VM._01(d))
不过我建议大家写成:
import VIP_module1 as VM
a=[[' ',' ',' ',' '],[' ',' ',' ',' '],[' ',' ',' ',' '],[' ',' ',' ',' ']]
b=[0,0,0,0]
c=['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16']
z=0
while True:
z+=1
if c==[]:
print('你胜利了,你的结果是:')
VM.print_xy_list(a)
print('你用了%d次。'%z)
break
d=VM.MAXorMIN('Min',VM.str_in_list2int_in_list(c))
VM.print_xy_list(a)
e=int(input('行数(1~4),要放"'+str(d)+'": '))
if b[e-1]==0:
a[e-1][0]=c[VM.Tfote(VM.str_in_list2int_in_list(c),d)-1]
b[e-1]+=d
c.remove(VM._01(d))
else:
if b[e-1]<d:
for i in a[e-1]:
if i!=' ':
c.append(i)
a[e-1]=[VM._01(d),' ',' ',' ']
c.remove(VM._01(d))
b[e-1]=d
elif VM.Tfote(a[e-1],' ')==None:
print('UNKNOWN ERROR: THE FORMAT IS OUT OF RANGE.')
break
else:
a[e-1][VM.Tfote(a[e-1],' ')-1]=VM._01(d)
b[e-1]+=d
c.remove(VM._01(d))
input('')
这样,就能在一些结束闪退的编辑器上看到结果了。
作者:初学者联盟的博客-CSDN博客