DAY07-字符串和常用的结构
1.跑马灯效果
这段代码也是看懂的,不知道这里的清屏函数的意义
# 字符串和常用的数据结构
# 跑马灯效果
#参考代码:https://www.cnblogs.com/68xi/p/8546443.html
import os
import time
def main():
str1='欢迎来到前锋学习Python'
while True:
os.system('cls') #清屏函数
print(str1)
time.sleep(0.5)
str1=str1[1:]+str1[0] #这个地方每次截取上一行输出从第二个字符开始
if __name__ == '__main__':
main()
2.列表找最大元素
这道题一开始我也走了弯路,因为直接可以利用列表的sort函数求出第一个的值就可
list2=[90,80,100,40]
list2.sort()
print("最大元素为",list2[-1])
list1=[90,80,100,40]
print("最大元素为",max(list1))
3.双色球
import random
def display(balls):
for x in range(len(balls)):
if x==len(balls)-1:
print("|",end=" ")
print("%d" %balls[x] ,end=" ")
print()
def ballsselect():
red_balls=[x for x in range(1,34)]
# selected_balls= []
selected_balls = random.sample(red_balls, 6) #这里运用了random模块中的sample函数,很简便的在指定序列中输出6个不同的数
selected_balls.sort()
selected_balls.append(random.randint(1,16))
return selected_balls
if __name__ == '__main__':
stack=int(input("请输入几注"))
for _ in range(stack):
display(ballsselect())
结果:
请输入几注4
1 4 9 21 30 32 | 4
2 7 8 10 15 26 | 6
2 3 4 14 16 22 | 15
1 2 9 11 13 25 | 6
4.井字棋
我以为的井字棋是我们小时候打的连成一条线的那种,看很多人写的井字棋似乎双方可以走不止 三步,一共只能走九步,所以这里并没有涉及到棋子移动的问题,那么这样的程序就稍稍简单了一点,以下是我参考的答案
# -*- coding: utf-8 -*-
import os
import sys
#棋盘模块
def model(dictionary,serial=False):
if serial:
print('-(初版)井字棋游戏,输入棋号进行对战,')
print('对应棋号为第一行:a1-a2-a3',end=',')
print('对应棋号为第二行:b1-b2-b3',end=',')
print('对应棋号为第三行:c1-c2-c3')
print(dictionary['a1'] + ' | '+ dictionary['a2'] +' | '+ dictionary['a3'] +' | ')
print('- +- +- +-')
print(dictionary['b1'] + ' | ' + dictionary['b2'] + ' | ' + dictionary['b3'] + ' | ')
print('- +- +- +-')
print(dictionary['c1'] + ' | ' + dictionary['c2'] + ' | ' + dictionary['c3'] + ' | ')
#主模块
def main():
dictionary={'a1':' ','a2':' ','a3':' ','b1':' ','b2':' ','b3':' ','c1':' ','c2':' ','c3':' '}
model(dictionary, True)
u1 = 'x' #用户1
u2 = 'o' #用户2
stepNumber =1 #记录步数
break_fang = 0 #获胜者记录
while(stepNumber<=9): ##判断下了几次棋
fv = True # 判断条件2
while fv:
num = input('请用户u1开始下棋:')
compare=1 #判断条件1
for x in dictionary:
if x.find(num)!=-1:compare=0
if compare ==0:
fv=False
dictionary[num] = u1
model(dictionary)
# 0:继续 1,用户1胜,2,用户2胜
break_fang = forResult(dictionary)
if break_fang > 0: break
fv =True #清楚状态
stepNumber+=1
while fv:
num1=input('请用户u2开始下棋:')
compare = 1 # 判断条件1
for x in dictionary:
if x.find(num1)!=-1:compare=0
if compare == 0:
fv=False
dictionary[num1] = u2
model(dictionary)
break_fang = forResult(dictionary)
if break_fang > 0: break
stepNumber+=1
gameover(break_fang)
#退出下棋
def gameover(break_fang):
c = input('是否重新开始? yes:no:')
if c.find('yes')!=-1:
main()
else:
print('-游戏结束-')
return
#判断获胜情况
#dictionary:棋盘信息
def forResult(dictionary):
dicts= dict(dictionary)
if dicts['a1'] == dicts['a2'] and dicts['a2'] == dicts['a3'] and len(dicts['a3'].strip())>0:
print('游戏结束,' + '用户1-获胜' if dicts['a1'] == 'x' else '用户2-获胜')
return 1 if dicts['a1']=='x' else 2
elif dicts['a1'] == dicts['b2'] and dicts['b2'] == dicts['c3'] and len(dicts['c3'].strip())>0:
print('游戏结束,' + '用户1-获胜' if dicts['a1'] == 'x' else '用户2-获胜')
return 1 if dicts['a1'] == 'x' else 2
elif dicts['a1'] == dicts['b1'] and dicts['b1'] == dicts['c1'] and len(dicts['c1'].strip())>0:
print('游戏结束,' + '用户1-获胜' if dicts['a1'] == 'x' else '用户2-获胜')
return 1 if dicts['a1'] == 'x' else 2
elif dicts['a2'] == dicts['b2'] and dicts['b2'] == dicts['c2'] and len(dicts['c2'].strip())>0:
print('游戏结束,' + '用户1-获胜' if dicts['a2'] == 'x' else '用户2-获胜')
return 1 if dicts['a2'] == 'x' else 2
elif dicts['a3'] == dicts['b3'] and dicts['b3'] == dicts['c3'] and len(dicts['c3'].strip())>0:
print('游戏结束,' + '用户1-获胜' if dicts['a3'] == 'x' else '用户2-获胜')
return 1 if dicts['a3'] == 'x' else 2
elif dicts['a3'] == dicts['b2'] and dicts['b3'] == dicts['c1'] and len(dicts['c1'].strip())>0:
print('游戏结束,' + '用户1-获胜' if dicts['a3'] == 'x' else '用户2-获胜')
return 1 if dicts['a3'] == 'x' else 2
elif dicts['b1'] == dicts['b2'] and dicts['b2'] == dicts['b3'] and len(dicts['b3'].strip())>0:
print('游戏结束,' + '用户1-获胜' if dicts['b1'] == 'x' else '用户2-获胜')
return 1 if dicts['b1'] == 'x' else 2
elif dicts['c1'] == dicts['c2'] and dicts['c2'] == dicts['c3'] and len(dicts['c3'].strip())>0:
print('游戏结束,' + '用户1-获胜' if dicts['c1'] == 'x' else '用户2-获胜')
return 1 if dicts['c1'] == 'x' else 2
else:
return 0
if __name__ =='__main__':
main()