# 猜字符游戏:猜测用户输入的字符是否是9527,限制只能猜3次。 number = 9527 init = 1 # 1 2 3 4 while True: # 若init大于3结束循环 if init > 3: print("猜错3次了,gun吧!") break guess = input("请输入猜测数字:") guess = int(guess) if guess == number: print("猜对了!") break elif guess < number: print("猜小了") else: print("猜大了!") # 若没有退出循环证明猜错了,init + 1 init +=1 # 0 1 2 3 4 # list1 = [1, 2, 3, 4, 5] ''' 需求: 循环取出列表list1的每一个值。 for循环: for x in 需要循环的值: for x in list1: # x=1, x=2, x=3, x=4, x=5 print(x) ''' list1 = [1, 2, 3, 4, 5] # list1_len = len(list1) # 5 # # while list1_len > 0: # list1_len -= 1 # # print(list1_len) # 4 3 2 1 0 # # print(list1[list1_len]) # 依次循环取出list1中的每一个值 for x in list1: print(x) str1 = '墙里的想着出去,墙外的人想着进来' # 01 2 34 5 6...89.16 # 索引取值 print(str1[9]) # 外 # 切片 # 获取str1中的"墙外的人"四个字 # range(8, 12) # 8-11 # [8:12] 顾头不顾尾 尾巴-1 print(str1[8:12]) # 墙外的人 # 长度len print(len(str1)) # 成员运算in|not in print('进' in str1) # True, 判断进字是否在str1里面 print('进' not in str1) # False # 移除空白strip username = input('请输入用户名') print(username) # 移除username两边的空格 username = username.strip() print(username) str2 = '墨:菲:定:律' # 切分split(切分的规则) # list1 = [] # for x in str2: # print(x) # list1.append(x) # print(list1) list1 = str2.split(':') print(list1) # lower&upper 大小写 str3 = 'aabbcc' # 把str3的字母都变成小写 lower_str3 = str3.lower() print(lower_str3) # aabbcc # 把str3的字母都变成大写 upper_str3 = str3.upper() print(upper_str3) # AABBCC # startswith&endswith 判断字符的开头或结尾是否是什么 str4 = 'tank是一个文质彬彬的靓仔' print(str4.startswith('tank')) # True print(str4.startswith('DSB')) # False print(str4.endswith('靓仔')) # True print(str4.endswith('sb')) # False # replace: 替换。replace(旧, 新) str5 = '吕连杰说,有个人很帅啊,他是徐峰啊!' str5 = str5.replace('徐峰','tank') print(str5) # isdigit: 判断字符是否是数字 str6 = '24124141' print(str6.isdigit()) # True list1 = ['tank',18,'tank'] #append list1.append('male') print(list1) #count:计算列表中值的数量 print(list1.count('tank')) # 2 str1 = '墙里的想着出去,墙外的人想着进来' # 01 2 34 5 6...89.16 # 索引取值 print(str1[9]) # 外 # 切片 # 获取str1中的"墙外的人"四个字 # range(8, 12) # 8-11 # [8:12] 顾头不顾尾 尾巴-1 print(str1[8:12]) # 墙外的人 # 长度len print(len(str1)) # 成员运算in|not in print('进' in str1) # True, 判断进字是否在str1里面 print('进' not in str1) # False # 移除空白strip username = input('请输入用户名') print(username) # 移除username两边的空格 username = username.strip() print(username) str2 = '墨:菲:定:律' # 切分split(切分的规则) # list1 = [] # for x in str2: # print(x) # list1.append(x) # print(list1) list1 = str2.split(':') print(list1) # lower&upper 大小写 str3 = 'aabbcc' # 把str3的字母都变成小写 lower_str3 = str3.lower() print(lower_str3) # aabbcc # 把str3的字母都变成大写 upper_str3 = str3.upper() print(upper_str3) # AABBCC # startswith&endswith 判断字符的开头或结尾是否是什么 str4 = 'tank是一个文质彬彬的靓仔' print(str4.starswich('tank')) # True print(str4.startswich('DSB')) # False print(str4.endswith('靓仔')) # True print(str4.endswith('sb')) # False # replace: 替换。replace(旧, 新) str5 = '吕连杰说,有个人很帅啊,他是徐峰啊!' str5 = str5.replace('徐峰','tank') print(str5) # isdigit: 判断字符是否是数字 str6 = '24124141' print(str6.isdigit()) # True list1 = ['tank',18, 'tank'] # append list1.append('male') print(list1) # count: 计算列表中值的数量 print(list1.count('tank')) # 2 ''' 字典的内置方法 ''' # dict1: # name = 'tank' # age = 18 dict1 = {'name': 'tank', 'age': 18} print(dict1['name']) # 若key不存在则报错 # print(dict1['name']) # get特点是key没有默认返回None print(dict1.get('name1')) # None ''' 文本读写 字符编码 美国: ASCCI 中国: GBK 日本... 总结: ''' # 写文件 # with open('安财贸.txt', 'w', encoding='utf-8) as f: # str1 = 'tank很帅,真的啊!' # f.write(str1) # 读文件 with open('安财贸.txt','r', encoding='utf-8') as f: str1 = f.read() print(str1) ''' 读写二进制流数据 ''' with open('xiao泽.mp4', 'r') as f: data = f.read() print(data) ''' 爬取cang老师图片,并保存到本地. 1.导入request模块 下载: 注意:必须联网 方式一: pip3 install requests 方式二: file->settings->project->interpreter->'绿色的' 在输入框内输入 ---> requests --> install package import requests 2.使用requests爬取图片并保存 - 1)下载图片 - 2)保存图片 ''' import requests # 1)下载图片 # requests.get('数据的连接') # 往cang老师图片地址发送请求获取响应数据 response = requests.get('https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=4c9bf08f04f41bd5da53eff269e1e6f6/d439b6003af33a87d8d517becc5c10385243b5dd.jpg') # content就是获取图片的二进制流数据 print(response.content) # 2)保存数据 # 读取二进制流数据 write + bytes wb with open('cang老师.jpg', 'wb') as f: # f.write(传入二进制流的数据) f.write(response.content)