from random import choice score_you = 0 score_com = 0 def judge(a, b, c, d, e): global score_com global score_you direction = ["左", "右", "中", "上", "下", "左上", "右上"] if e == 1: dire = direction[:3] elif e == 2: dire = direction[:-2] else: dire = direction print(d + "\n") print("选择你要" + a + "的方向:",end="") print(dire) you = input() print("你" + b + "向了" + you + "方向") com = choice(dire) print("电脑选择了" + com + "方向") if you != com: print(c + "进球了!") if c == "电脑": score_com += 1 else: score_you += 1 else: print("差点意思.") diff = int(input("请选择难度系数:(请输入数字1~3)")) ju = int(input("请选择先攻先守:(请输入数字:0.先攻 1.先守)")) for i in range(0, 6): if ju: a = "防守" b = "扑" c = "电脑" d = "------------你防守电脑进攻篇------------" ju = 0 else: a = "进攻" b = "踢" c = "你" d = "------------你进攻电脑防守篇------------" ju = 1 judge(a, b, c, d, diff) print("最终得分:电脑:%d , 你:%d" % (score_com, score_you))
以上程序中所用到的知识点如下:
- 输出结果在同一行
for i in range(0, 5):
print('*', end=' ')
#加上end参数
- 数据结构-list
#range(1, 10)等价于[1, 2, 3, 4, 5, 6, 7, 8, 9]
#list内部可以是不同数据类型的混合,l = [1, "jack", 3, 4, "rose", 6, 7, 8, 9]
#此数据结构可被for循环遍历
- list的数据操作
#访问、修改list中的元素方式与数组同
#l[-1]表示l中的最后一个元素
#添加元素 l.append(添加内容)
#删除元素 del l[0]
- list的切片
#切片操作 l[1:3]
#冒号前后的数分别表示切片起末位置
#开始位置包含在切片中,结束位置不包括
- 从list中随机选择
#random模块内的choice方法可以在list中随机选值
- 全局变量
#如果在函数中给全局变量赋值,需要用global关键字声明
#否则全局变量将被函数中局部变量覆盖,出现error