record-05 函数

练习:点球大战游戏(基于之前做的猜数字游戏思路)
足球比赛经过90分钟的赛事后,如果比分相同,则双方各派5名队员进行点球大战
5轮点球大战双方交替进行,即在1轮比赛中双方各射门一次、守门一次,5轮比赛 结束后对比双方得分确定获胜方
如果5轮比赛后比分相同,则进行第6轮比赛,如果第6轮比分还相同,那么进入下一轮
在5轮比赛过程中,如果能提前确定获胜方,则提前结束比赛


# 实现一个点球游戏 from random import randint user_score = 0 com_score = 0 # 两只队伍各派5名队员参与点球 # 点球共分5轮 # 每一轮,双方各射门一次,守门一次 for i in range(1, 6): print("当前是第%d轮比赛" % i) # 用户先射门,程序守门 # 程序先提示用户可以射门了,并且要求用户选择一个射门方向 print("请选择射门方向:") # 用户选择射门方向 # 程序通过接收,明确用户选择的方向是什么 user = int(input()) # 程序选择一个守门方向 com = randint(0, 1) # 判断方向是否一致 # 如果方向一致,代表球没进,用户不得分 # 如果方向不一致,代表球进了,用户得分+1 if user == com: print("球没进") user_score = user_score + 0 else: print("球进了") user_score = user_score + 1 print("当前双方比分用户:程序 %d:%d" % (user_score, com_score)) # 程序后射门,用户守门 # 程序先提示用户可以守门了,并且要求用户选择一个守门方向 print("请选择守门方向:") # 用户选择守门方向 # 程序通过接收,明确用户选择的方向是什么 user = int(input()) # 程序选择一个射门方向 com = randint(0, 1) # 判断方向是否一致 # 如果方向一致,代表球没进,程序不得分 # 如果方向不一致,代表球进了,程序得分+1 if user == com: print("球没进") com_score = com_score + 0 else: print("球进了") com_score = com_score + 1 print("当前双方比分用户:程序 %d:%d" % (user_score, com_score)) # 实现一个点球游戏


如果5轮比赛后比分相同,则进行第6轮比赛,如果第6轮比分还相同,那么进入下一轮

from random import randint

user_score = 0
com_score = 0
# 两只队伍各派5名队员参与点球
# 点球共分5轮
# 每一轮,双方各射门一次,守门一次

for i in range(1, 6):
    print("当前是第%d轮比赛" % i)

    # 用户先射门,程序守门
    # 程序先提示用户可以射门了,并且要求用户选择一个射门方向
    print("请选择射门方向:")
    # 用户选择射门方向
    # 程序通过接收,明确用户选择的方向是什么
    user = int(input())
    # 程序选择一个守门方向
    com = randint(0, 1)
    # 判断方向是否一致
    # 如果方向一致,代表球没进,用户不得分
    # 如果方向不一致,代表球进了,用户得分+1
    if user == com:
        print("球没进")
        user_score = user_score + 0
    else:
        print("球进了")
        user_score = user_score + 1
    print("当前双方比分用户:程序 %d:%d" % (user_score, com_score))

    # 程序后射门,用户守门
    # 程序先提示用户可以守门了,并且要求用户选择一个守门方向
    print("请选择守门方向:")
    # 用户选择守门方向
    # 程序通过接收,明确用户选择的方向是什么
    user = int(input())
    # 程序选择一个射门方向
    com = randint(0, 1)
    # 判断方向是否一致
    # 如果方向一致,代表球没进,程序不得分
    # 如果方向不一致,代表球进了,程序得分+1
    if user == com:
        print("球没进")
        com_score = com_score + 0
    else:
        print("球进了")
        com_score = com_score + 1
    print("当前双方比分用户:程序 %d:%d" % (user_score, com_score))

# 当5轮比赛后,双方比分相同,进行后续比赛
times = 6
while user_score == com_score:
    print("当前是第%d轮比赛" % times)
    # 用户先射门,程序守门
    # 程序先提示用户可以射门了,并且要求用户选择一个射门方向
    print("请选择射门方向:")
    # 用户选择射门方向
    # 程序通过接收,明确用户选择的方向是什么
    user = int(input())
    # 程序选择一个守门方向
    com = randint(0, 1)
    # 判断方向是否一致
    # 如果方向一致,代表球没进,用户不得分
    # 如果方向不一致,代表球进了,用户得分+1
    if user == com:
        print("球没进")
        user_score = user_score + 0
    else:
        print("球进了")
        user_score = user_score + 1
    print("当前双方比分用户:程序 %d:%d" % (user_score, com_score))

    # 程序后射门,用户守门
    # 程序先提示用户可以守门了,并且要求用户选择一个守门方向
    print("请选择守门方向:")
    # 用户选择守门方向
    # 程序通过接收,明确用户选择的方向是什么
    user = int(input())
    # 程序选择一个射门方向
    com = randint(0, 1)
    # 判断方向是否一致
    # 如果方向一致,代表球没进,程序不得分
    # 如果方向不一致,代表球进了,程序得分+1
    if user == com:
        print("球没进")
        com_score = com_score + 0
    else:
        print("球进了")
        com_score = com_score + 1
    print("当前双方比分用户:程序 %d:%d" % (user_score, com_score))

    times = times + 1

# 实现一个点球游戏
在5轮比赛过程中,如果能提前确定获胜方,则提前结束比赛 from random import randint user_score = 0 com_score = 0 # 两只队伍各派5名队员参与点球 # 点球共分5轮 # 每一轮,双方各射门一次,守门一次 for i in range(1, 6): print("当前是第%d轮比赛" % i) # 用户先射门,程序守门 # 程序先提示用户可以射门了,并且要求用户选择一个射门方向 print("请选择射门方向:") # 用户选择射门方向 # 程序通过接收,明确用户选择的方向是什么 user = int(input()) # 程序选择一个守门方向 com = randint(0, 1) # 判断方向是否一致 # 如果方向一致,代表球没进,用户不得分 # 如果方向不一致,代表球进了,用户得分+1 if user == com: print("球没进") user_score = user_score + 0 else: print("球进了") user_score = user_score + 1 print("当前双方比分用户:程序 %d:%d" % (user_score, com_score)) # 判断是否能提前结束比赛 # 3:0 4:1 4:2 # 领先方领先的分数比落后方还能获取的最大分数大,可以确定提前结束比赛 if user_score > com_score: if user_score - com_score > 5 - i + 1: break else: if com_score - user_score > 5 - i: break # 程序后射门,用户守门 # 程序先提示用户可以守门了,并且要求用户选择一个守门方向 print("请选择守门方向:") # 用户选择守门方向 # 程序通过接收,明确用户选择的方向是什么 user = int(input()) # 程序选择一个射门方向 com = randint(0, 1) # 判断方向是否一致 # 如果方向一致,代表球没进,程序不得分 # 如果方向不一致,代表球进了,程序得分+1 if user == com: print("球没进") com_score = com_score + 0 else: print("球进了") com_score = com_score + 1 print("当前双方比分用户:程序 %d:%d" % (user_score, com_score)) # 判断是否能提前结束比赛 if user_score > com_score: if user_score - com_score > 5 - i and 5 - i != 0: break else: if com_score - user_score > 5 - i and 5 - i != 0: break # 当5轮比赛后,双方比分相同,进行后续比赛 times = 6 while user_score == com_score: print("当前是第%d轮比赛" % times) # 用户先射门,程序守门 # 程序先提示用户可以射门了,并且要求用户选择一个射门方向 print("请选择射门方向:") # 用户选择射门方向 # 程序通过接收,明确用户选择的方向是什么 user = int(input()) # 程序选择一个守门方向 com = randint(0, 1) # 判断方向是否一致 # 如果方向一致,代表球没进,用户不得分 # 如果方向不一致,代表球进了,用户得分+1 if user == com: print("球没进") user_score = user_score + 0 else: print("球进了") user_score = user_score + 1 print("当前双方比分用户:程序 %d:%d" % (user_score, com_score)) # 程序后射门,用户守门 # 程序先提示用户可以守门了,并且要求用户选择一个守门方向 print("请选择守门方向:") # 用户选择守门方向 # 程序通过接收,明确用户选择的方向是什么 user = int(input()) # 程序选择一个射门方向 com = randint(0, 1) # 判断方向是否一致 # 如果方向一致,代表球没进,程序不得分 # 如果方向不一致,代表球进了,程序得分+1 if user == com: print("球没进") com_score = com_score + 0 else: print("球进了") com_score = com_score + 1 print("当前双方比分用户:程序 %d:%d" % (user_score, com_score)) times = times + 1 if user_score > com_score: print("恭喜,用户获胜") else: print("很遗憾,程序获胜") # 实现一个点球游戏 from random import randint def u_shemen(): # global user_score, com_score # 用户先射门,程序守门 # 程序先提示用户可以射门了,并且要求用户选择一个射门方向 print("请选择射门方向:") # 用户选择射门方向 # 程序通过接收,明确用户选择的方向是什么 user = int(input()) # 程序选择一个守门方向 com = randint(0, 1) # 判断方向是否一致 # 如果方向一致,代表球没进,用户不得分 # 如果方向不一致,代表球进了,用户得分+1 if user == com: print("球没进") user_score = user_score + 0 else: print("球进了") user_score = user_score + 1 print("当前双方比分用户:程序 %d:%d" % (user_score, com_score)) def c_shemen(): #函数重用性 global user_score, com_score user_score = 0 com_score = 0 # 程序后射门,用户守门 # 程序先提示用户可以守门了,并且要求用户选择一个守门方向 print("请选择守门方向:") # 用户选择守门方向 # 程序通过接收,明确用户选择的方向是什么 user = int(input()) # 程序选择一个射门方向 com = randint(0, 1) # 判断方向是否一致 # 如果方向一致,代表球没进,程序不得分 # 如果方向不一致,代表球进了,程序得分+1 if user == com: print("球没进") com_score = com_score + 0 else: print("球进了") com_score = com_score + 1 print("当前双方比分用户:程序 %d:%d" % (user_score, com_score)) user_score = 0 com_score = 0 # 两只队伍各派5名队员参与点球 # 点球共分5轮 # 每一轮,双方各射门一次,守门一次 for i in range(1, 6): print("当前是第%d轮比赛" % i) u_shemen() # 判断是否能提前结束比赛 # 3:0 4:1 4:2 # 领先方领先的分数比落后方还能获取的最大分数大,可以确定提前结束比赛 if user_score > com_score: if user_score - com_score > 5 - i + 1: break else: if com_score - user_score > 5 - i: break c_shemen() # 判断是否能提前结束比赛 if user_score > com_score: if user_score - com_score > 5 - i and 5 - i != 0: break else: if com_score - user_score > 5 - i and 5 - i != 0: break # 当5轮比赛后,双方比分相同,进行后续比赛 times = 6 while user_score == com_score: print("当前是第%d轮比赛" % times) u_shemen() c_shemen() times = times + 1 if user_score > com_score: print("恭喜,用户获胜") else: print("很遗憾,程序获胜") # 某个公司传输数据,数据是四位的整数,在传递过程中是加密的 # 加密规则如下:每位数字都加上5,然后再用除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换 # 设计解密函数,能够根据接收的数据判断出原始数据 def jiemi(new): # 解密 # 1、接收准备解密的数据 # 2、将加密后的数据其中的一、二、三、四位找出来 a = new // 1000 b = new % 1000 // 100 c = new % 100 // 10 d = new % 10 # 3、将第一位和第四位交换,第二位和第三位交换 a, d = d, a b, c = c, b # 4、原数据为2,加密后位7 原数据为8,加密后位3 # 5、规律:如果加密后的数据大于5,将其-5即为原数据 # 6、规律:如果加密后的数据小于5,将其+5即为原数据 if a >= 5: a = a - 5 else: a = a + 5 if b >= 5: b = b - 5 else: b = b + 5 if c >= 5: c = c - 5 else: c = c + 5 if d >= 5: d = d - 5 else: d = d + 5 # 7、将每一位的原数据组成4位整数 result = a * 1000 + b * 100 + c * 10 + d return result # 原5555 密0000 # 原0000 密5555 # 原0987 密2345 x = jiemi(2345) print(x) #函数的作用:增强代码重用性、提高代码设计效率和执行效率、降低代码维护成本 #使用函数的思路 #确定函数要完成什么样的工作,并根据要完成的工作明确函数体中应该包含哪些命令 #根据函数体代码的执行,来确定是否需要有参数列表 #根据函数的调用,来确定是否需要有返回值 #函数定义 def关键字 #函数名称,命名规则同变量名,并且同一个源代码文件中,函数名不能相同 #函数在定义时,可能会有参数列表,参数列表中每一个都叫形参 #形参并不代表某一个数据,而是表示函数需要从函数外接收一个数据 #并且形参在接收这个数据后,也会将数据临时存储起来 x=1 #全局变量 y=2 #全局变量 z=3 #全局变量 def f(): global x x=100 #修改全局变量的值 y=200 #定义局部变量 z=300 #定义局部变量 print(x,y,z) #输出全局变量x,局部变量y和z的值 #函数调用 #只有通过函数调用,才能让函数体中的命令生效 #函数调用时,及时不需要接收参数,括号也不能省略 f() print(x,y,z) #输出全局变量x,y和z的值 #全局变量、局部变量 #根据变量的使用范围来确定应该用全局变量,还是局部变量 #如果一个变量,只需要在函数体中使用,在函数体外不会用到,那么它就应该被定义为局部变量 #如果一个变量,即在函数体中使用,又在函数体外使用,那么它就应该被定义为全局变量 #如果全局变量,在函数体中想要重新赋值,一定要在函数体中使用global #默认值参数 在函数定义时使用 #没有使用默认值参数时,函数有几个形参,就需要在调用时,传递几个实参 #默认值参数只能出现在参数列表的后面 def f2(x,y=0,z=1): print(x+2*y+3*z) f2(1) f2(1,2) f2(1,2,3) #关键参数 在函数调用时使用 #没有使用关键参数,函数的实参默认是按照顺序传递给形参 f2(1,2,3) f2(3,2,1) #按照关键字传递实参 f2(x=1,y=2,z=3) f2(z=3,x=1,y=2) f2(1,z=3,y=2) print("hello",sep=' ',end=' ') #return 返回值 #退出函数的执行 #从函数中返回值,返回到程序中函数被调用的位置 #当函数体中没有明确使用return时,默认返回None值 def f3(x,y): return 10*x+20*y

  

原文地址:https://www.cnblogs.com/minkillmax/p/8261043.html