2019.8.6 learning content and notes

summary

python copy depth

# 拷贝(赋值)
# l1 = [1,2,3]
# l2 = l1
# l1.append(4)
# print(l1)
# print(l2)
# 因为列表是可变类型,所以l1的值发生变化,l2的值也会跟着变化(这就是拷贝)

# 浅拷贝
# import copy #
# l1 = [1,2,3,4,[5,6,7]]
# l2 = copy.copy(a) #(浅拷贝)
# l1.append(8)
# print(l1) #
# print(l2) #
# l1[4].append(8)
# print(l1)
# print(l2)
# 当b为a的浅拷贝对象时,a内可变类型变化,b变化,a内不可变型变化,b不变化

# 深拷贝
# import copy
# l1 = [1,2,3,[4,5,6]]
# l2 = copy.deepcopy(l1)
#l1.append(7)
#print(l1)
#print(l2)
# s[3].append(7)
# print(l1)
# print(l2)
# 注:当l2为l1的深拷贝对象时,l1内的可变类型变化,l2不变化;l1的不可变型变化;l2不变化
# 总结
# 牢记:拷贝、浅拷贝、深拷贝  只针对可变数据类型
# 拷贝: 当lt2为lt的拷贝对象时,lt内的可变类型变化,lt2变化;lt内的不可变类型变化,lt2变化
# 简单的赋值

# 浅拷贝:当lt2为lt的浅拷贝对象时,lt内的可变类型变化,lt2变化;lt内的不可变类型变化,lt2不变化
# copy.copy()   可变数据的类型中的内置方法.copy()

# 深拷贝: 当lt2为lt的深拷贝对象时,lt内的可变类型变化,lt2不变化;lt内的不可变类型变化,lt2不变
# copy.deepcopy()
### 作用:毫无用处,面试经常问

The basic document processing

What is file: operating system provides a virtual units to customers

The role of file: data storage

Open the process file

  1. Locate the file path # Right-click on the file, copy path
path = r'r'E:\老男孩学习笔记\7.29学习内容\01 执行Python程序的两种方式'
  1. Double-click to open
f = open(path,'w') # r-->read 只读; w-->只写,清空当前文件后写入,自动创建文件
print(f)
  1. See the file
data = f.read()

print(data)
  1. Write file
f.write('nick handsome')
  1. Close the file

Note: Use del f, only to delete the reference to the file and the file in python memory footprint, but did not remove the occupancy of the operating system

With f.close () to shut down the operating system takes up files

Absolute and relative paths

Absolute path: from the start with the directory (C / D / E disk), this is called an absolute path

path = r'r'E:\老男孩学习笔记\7.29学习内容\01 执行Python程序的两种方式'
f = open(path,'w')
f.write('nick handsome')
f.close() #关闭文件

:( relative path to the current folder, you can write directly to the file name)

path = r'test.py' #鼠标右键点击文件,copy path
f = open(path,'r')
print(f.read)
f.close() # 关闭文件

Exception Handling

Exception: error is

The syntax and logic into abnormal abnormal abnormal

Syntax exception: syntax errors are the exception (SyntaxError)

Find abnormalities:

try:
    if
 except Exception as e:  # 语法异常无法捕捉
    print(e)

Logical anomaly: Error often not the same

 print(1)
# try:  # 尝试
#     print(3)  # 代码自上而下,上面会运行
#     1 / 0  # 错误不影响其他的代码,报错马上终止try缩进里面的代码
#     print(4)
# except:  # 除此之外
#     pass
# 1/0
# print(2)
print(1)
# try:  # 尝试
#     num = input('输入一个值')  # 123124
#     dic = {'0': 'a'}
#     print(dic[num])  # dic['123124']
#
#     print(3)  # 代码自上而下,上面会运行
#     1 / int(num)  # 错误不影响其他的代码,报错马上终止try缩进里面的代码
#     print(4)
except ZeroDivisionError as e:  # 除此之外  # try里面的代码出了什么错误,就得用什么错误捕捉  # as是把错误赋值给e#     print('e:', e)# except KeyError as e:  # 可以写多个except捕捉多个异常#     print('e:', e)

Most important, after trying to capture abnormal use this, just remember that this is enough, as follows

print(1)
# try:  # 尝试
#     num = input('输入一个值')  # 123124
#     dic = {'0': 'a'}
#     print(dic[num])  # dic['123124']
#
#     print(3)  # 代码自上而下,上面会运行
#     1 / int(num)  # 错误不影响其他的代码,报错马上终止try缩进里面的代码
#     print(4)
#

# except Exception as e:  # Exception可以捕捉任意异常
#     print('e:', e)
#
# # 1/0
# print(2)

Data type classification

According to the number stored-value

A stored value: integer, floating point, string

A plurality of stored values: lists, dictionaries, set, tuple

An ordered or disordered:

Ordered: strings, lists, tuples

Disorder: dictionaries, collections

At a variable or non-variable:

Variable: lists, sets, dictionaries

Immutable: integer, floating point, tuple string

TODO usage

if true:           
   TODO :
  pass

In the python interpreter appears below the TODO, click to jump directly to the premises

I guess the age of the game

# 0. 奖励物品存放在文件price.txt
#
# 1. 给定年龄(随机18-60),用户可以猜三次年龄
#
# 2. 年龄猜对,让用户选择两次奖励
#
# 3. 用户选择两次奖励后可以退出
# '''
#
# import random
#
# age = random.randint(18, 60)  # 随机一个数字,18-60岁
# count = 0  # 计数
#
# f = open('price.txt', 'r', encoding='utf8')  # price.txt右下角为什么编码,则encoding为什么编码
# price_dict = f.read()
# price_dict = eval(price_dict)  # type:dict # 获取奖品字典
# f.close()
#
# price_self = dict()
#
# while count < 3:
#     count += 1
#
#     inp_age = input('请输入你想要猜的年龄:')
#
#     # 判断是否为纯数字
#     if not inp_age.isdigit():
#         print('搞事就骂你傻逼')
#         continue
#
#     inp_age = int(inp_age)
#
#     # 筛选年龄范围
#     if inp_age > 60 or inp_age < 18:
#         print('好好题目,18-60岁,非诚勿扰')
#         continue
#
#     # 核心逻辑
#     if age == inp_age:
#         print('猜中了,请选择你的奖品')
#
#         # 打印商品
#         for k, v in price_dict.items():
#             print(f'奖品编号:{k} {v}')
#
#         # 获取奖品的两次循环
#         for i in range(2):
#             price_choice = input('请输入你需要的奖品编号:')
#
#             if not price_choice.isdigit():
#                 print("恭喜你已经获得一次奖品,奖品为空!并且请输入正确的奖品编号!")
#                 continue
#
#             price_choice = int(price_choice)
#
#             if price_choice not in price_dict:
#                 print('你想多了吧!')
#             else:
#                 price_get = price_dict[price_choice]
#                 print(f'恭喜中奖:{price_get}')
#
#                 if price_self.get(price_get):
#                     price_self[price_get] += 1
#                 else:
#                     price_self[price_get] = 1
#
#         print(f'恭喜你获得以下奖品:{price_self}')
#         break
#
#     elif age > inp_age:
#         print('猜小了')
#     elif age < inp_age:
#         print('猜大了')
#
#     continue






'''
0. 奖励物品存放在文件price.txt

1. 给定年龄(随机18-60),用户可以猜三次年龄

2. 年龄猜对,让用户选择两次奖励

3. 用户选择两次奖励后可以退出
'''

import random

age = random.randint(18, 60)  # 随机一个数字,18-60岁
count = 0  # 计数

f = open('price.txt', 'r', encoding='utf8')  # price.txt右下角为什么编码,则encoding为什么编码
price_dict = f.read()
price_dict = eval(price_dict)  # type:dict # 获取奖品字典
f.close()

price_self = dict()

while count < 3:
    count += 1

    inp_age = input('请输入你想要猜的年龄:')

    # 判断是否为纯数字
    if not inp_age.isdigit():
        print('搞事就骂你傻逼')
        continue

    inp_age = int(inp_age)

    # 筛选年龄范围
    if inp_age > 60 or inp_age < 18:
        print('好好题目,18-60岁,非诚勿扰')
        continue

    # 核心逻辑
    if age == inp_age:
        print('猜中了,请选择你的奖品')

        # 打印商品
        for k, v in price_dict.items():
            print(f'奖品编号:{k} {v}')

        # 获取奖品的两次循环
        for i in range(2):
            price_y = input(f'请按"Y or y"转动转盘{chr(9803)}:').lower()

            if price_y != 'y':
                print("恭喜你已经获得一次奖品,奖品为空!并且请输入'Y or y'!")
                continue

            # 
            price_choice = random.randint(0, 10000)

            if price_choice > 0 and price_choice < 9900:
                price_choice = 6
                print('恭喜你, 下次一定有好东西!!', end=' ')
            else:
                price_choice = price_choice % 7

            if price_choice not in price_dict:
                print('你想多了吧!')
            else:
                price_get = price_dict[price_choice]
                print(f'恭喜中奖:{price_get}')

                if price_self.get(price_get):
                    price_self[price_get] += 1
                else:
                    price_self[price_get] = 1

        print(f'恭喜你获得以下奖品:{price_self}')
        break

    elif age > inp_age:
        print('猜小了')
    elif age < inp_age:
        print('猜大了')

    continue

Guess you like

Origin www.cnblogs.com/chmily/p/11317977.html