Python学习笔记-基础语法1、int()函数:,我来为大家讲解一下关于2022年5月19日学习笔记-基础语法?跟着小编一起来看一看吧!

2022年5月19日学习笔记-基础语法(2022年5月19日学习笔记-基础语法)

2022年5月19日学习笔记-基础语法

Python学习笔记-基础语法

第七章 用户输入和while循环一、input()函数

1、int()函数:

age = input("请输入你的年龄:") print(age >= 19) 输出结果为: 请输入你的年龄:11 Traceback (most recent call last): File "D:\Python_Work\Python学习\Python编程-从入门到实践\练习.py", line 2, in <module> print(age >= 19) TypeError: '>=' not supported between instances of 'str' and 'int'

2、求模求商

方法

描述

%

求模,取余数。可取2模以判断奇偶

//

求商

>>> 10 // 2 5 >>> 10 % 2 0

二、while循环

prompt = "\nTell me something, and I will repeat it back to you: " prompt = "\nEnter 'quit' to end the program. " "\n" message = '' while message != 'quit': message = input(prompt) if message != 'quit': print(message) 输出结果为: Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. 你好帅啊 你好帅啊 Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. quit

1、使用标志高效运行while循环

prompt = "\nTell me something, and I will repeat it back to you: " prompt = "\nEnter 'quit' to end the program. " "\n" active = True #标志 while active: message = input(prompt) if message == 'quit': active = False else: print(message) 输出结果: Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. 你好帅啊 你好帅啊 Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. quit

2、使用break退出循环

prompt = "\nTell me something, and I will repeat it back to you: " prompt = "\nEnter 'quit' to end the program. " "\n" while True: city = input(prompt) if city == 'quit': break 输出结果为: Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. 你好帅啊 Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. quit

3、在循环中使用continue

current_number = 0 while current_number < 10: current_number = 1 if current_number %2 == 0: continue #如果符合条件测试,continue让他回到while重新执行,直到不符合条件测试,打印current_number print(current_number) 输出结果为: 1 3 5 7 9

三、使用while循环来处理列表和字典

1、在列表之间移动元素

unconfirmed_users = ['alice','brian','candace'] confirmed_users = [] while unconfirmed_users: #列表空则为False current_user = unconfirmed_users.pop() print("Verifying user: " current_user.title()) confirmed_users.append(current_user) print("\nThe following users have been confirmed: ") for confirmed_user in confirmed_users: print(confirmed_user) 输出结果为: Verifying user: Candace Verifying user: Brian Verifying user: Alice The following users have been confirmed: candace brian alice

2、删除包含特定值得所有列表元素

pets = ['dog','cat','dog','goldfish','cat','rabbit','cat'] print(pets) while 'cat' in pets: pets.remove('cat') print(pets) 输出结果为: ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat'] ['dog', 'dog', 'goldfish', 'rabbit']

3、使用用户输入来填充字典

responses = {} polling_active = True while polling_active: name = input("\nWhat is your name?\n ") response = input("Which mountain would you like to climb someday? \n") responses[name] = response repeat = input("Would you like to let another person respond? (yes/no) \n") if repeat == 'no': polling_active = False print("\n--- Poll Results ---") for name, response in responses.items(): print("\n" name.title() " would like to climb " response ".\n") 输出结果为: What is your name? 小明 Which mountain would you like to climb someday? 黄山 Would you like to let another person respond? (yes/no) yes What is your name? 小刚 Which mountain would you like to climb someday? 泰山 Would you like to let another person respond? (yes/no) no --- Poll Results --- 小明 would like to climb 黄山. 小刚 would like to climb 泰山.

,