赞
踩
在传统的观念中,函数包括自变量和因变量,如:y = f(x)或z = f(x,y) ;而在Python中,自变量被称为参数,因变量被称为返回值。
函数—> y = f(x)/ z = f(x,y) / … —>相对独立且会被重复使用的功能(代码),将来想使用这些功能的时候,不需要再复制粘贴代码,而是直接通过调用函数来做到。
全局变量:没有写在任何函数里面的变量
局部变量:定义在函数内部的变量
Python程序中搜索一个变量是按照LEGB顺序进行搜索
Local(局部作用域) —> Embeded(嵌套作用域) —> Global(全局作用域)—> Built-in(内置作用域)
global:生命使用全局变量或者定一个局部变量将其放到全局作用域
nonlocal:声明使用嵌套作用域的变量x(不使用局部变量)
""" example01 - 全局变量和局部变量 Author: Asus Date: 2021/8/2 """ x = 100 def foo(): # 如果我不想在函数foo中定义局部变量x,想直接使用全局变量x global x x = 200 def bar(): # 如果我不想在函数bar中定义局部变量x,想直接使用嵌套作用域的变量x # nonlocal x x = 300 print(x) # 300 bar() print(x) #200 foo() print(x) # 如果没有定义x=100,那么将会报错:name 'x' is not defined
1.求阶乘
""" example02 - 求阶乘 Author: Asus Date: 2021/8/2 """ def fac(num): """ 求阶乘 :param num: 所求阶乘的数 :return: 阶乘的结果 """ result = 1 for i in range(2, num + 1): result *= i return result m = int(input('m = ')) n = int(input('n = ')) print(fac(m) // fac(n) // fac(m - n))
2.CRAPS赌博游戏 —> 色子游戏
规则:
玩家摇两颗色子,如果第一次摇出了7点或11点,玩家胜;如果摇出了2点、3点、12点,庄家胜;
如果摇出了其他的点数,游戏继续,玩家重新摇色子;如果玩家摇出了第一次摇的点数,玩家胜;
如果玩家摇出了7点,庄家胜;如果玩家摇出其他点数,游戏继续,玩家重新摇色子,直到分出胜负。
游戏开始之前,玩家有1000元的初始资金,玩家可以下注,赢了获得下注的金额,输了就扣除下注的金额。
游戏结束的条件是玩家把钱输光。
""" homework01 - CRAPS赌博游戏 ---> 色子游戏 Author: Asus Date: 2021/8/2 """ import random def roll_dice(num=2): """ 摇色子 :param num: 色子的数量(对子变量进行说明) :return: 摇出的点数(对因变量进行说明) """ point = 0 for _ in range(num): point += random.randrange(1, 7) return point def win(): """玩家胜""" global counter print('玩家胜') counter += count def lose(): """玩家输""" global counter print('庄家胜') counter -= count counter = 1000 while counter > 0: print(f'玩家总资产{counter}元.') count = 0 while count <= 0 or count > counter: try: count = int(input('玩家下注的金额:')) except: pass # first_point = random.randrange(1, 7) + random.randrange(1, 7) # 因为函数的参数有默认值,调用函数时如果没有给自变量,就会使用默认值 first_point = roll_dice() print(f'玩家摇出了{first_point}点.') if first_point in (7, 11): win() # print(counter) elif first_point in (2, 3, 12): lose() # print(counter) else: while True: # curr_point = random.randrange(1, 7) + random.randrange(1, 7) curr_point = roll_dice() if curr_point == first_point: win() break elif curr_point == 7: lose() break if counter == 0: print('玩家破产,游戏结束')
3.双色球、大乐透随机选号
""" homework02 - 双色球、大乐透随机选号(实现机选N注) Author: Asus Date: 2021/8/2 """ import random def generate(red_ball_max=33, red_ball_num=6, blue_ball_max=16, blue_ball_num=1): """ 生成一组彩票号码 :param red_ball_max: 红色球的最大值 :param red_ball_num: 红色球的数量 :param blue_ball_max: 蓝色球的最大值 :param blue_ball_num: 蓝色球的数量 :return: 保存一组彩票号码的列表 """ red_balls = [i for i in range(1, red_ball_max + 1)] bule_balls = [i for i in range(1, blue_ball_max + 1)] selected_red_balls = random.sample(red_balls, red_ball_num) selected_red_balls.sort() # selected_balls.append(random.choice(bule_balls)) selected_blue_balls = random.sample(bule_balls, blue_ball_num) selected_blue_balls.sort() return selected_red_balls + selected_blue_balls def display(balls): """ 显示一组彩票号码 :param balls: 装彩票号码对应的球的列表 :return: """ for ball in balls: print(f'{ball:0>2d}', end=' ') print() n = int(input('机选几注:')) print('双色球玩法:') for _ in range(n): display(generate()) print('大乐透玩法:') for _ in range(n): display(generate(35, 5, 12, 2))
优化代码:
""" homework02 - 双色球、大乐透随机选号(实现机选N注) Author: Asus Date: 2021/8/2 """ import random def generate(ball_max, ball_num): """选择一组随机的球 :param ball_max: 球的最大值 :param ball_num: 球的数量 :return: 保存一组球的列表 """ balls = [i for i in range(1, ball_max + 1)] selected_balls = random.sample(balls, ball_num) selected_balls.sort() return selected_balls def make_big_lottery(): """生成大乐透的号码""" return generate(35, 5) + generate(12, 2) def make_two_colors(): """生成双色球的号码""" return generate(33, 6) + generate(16, 1) def display(balls): """ 显示一组彩票号码 :param balls: 装彩票号码对应的球的列表 """ for ball in balls: print(f'{ball:0>2d}', end=' ') print() n = int(input('机选几注: ')) print('双色球: ') for _ in range(n): display(make_two_colors()) print('大乐透: ') for _ in range(n): display(make_big_lottery())
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。