2022-07-20 Python学习条记8

分享
程序员 2024-9-29 15:54:42 71 0 来自 中国
一、总体筹划:

开始时间:2022-07-13
筹划完成时间:2022-08-12
筹划逐日完成量:15页/天,或0.7章/天
二、本日(2022-07-20)学习进度:

本日已学习24页,完成1章,日任务达标。
总体进度137页/460页,8章/20章。
三、学习条记:

1、函数input() 的工作原理的工作原理,函数input() 让程序停息运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中,以方便你使用。
message = input("Tell me something, and I will repeat it back to you: ")print(message)函数input() 继承一个参数:即要向用户体现的提示提示 或分析,让用户知道该怎样做。在这个示例中,Python运行第1行代码时,用户将看到提示Tell me something, and I will repeat it back to you: 。程序等待用户输入,并在用户按回车键后继续运行。输入存储在变量message 中,接下来的print(message) 将输入出现给用户:
Tell me something, and I will repeat it back to you: Hello everyone!Hello everyone!2、使用int() 来获取数值输入,使用函数input() 时,Python将用户输入解读为字符串,可使用函数int() ,它让Python将输入视为数值。函数int() 将数字的字符串表现转换为数值表现。
>>> age = input("How old are you? ")  How old are you? 21>>> age = int(age)  >>> age >= 18  True3、求模运算符,处置惩罚数值信息时,求模运算符求模运算符 (%)是一个很有用的工具,它将两个数相除并返回余数。
>>> 4 % 31>>> 5 % 32>>> 6 % 30>>> 7 % 314、判断一个数是奇数照旧偶数,偶数都能被2整除,因此对一个数(number )和2实验求模运算的效果为零,即number % 2 == 0 ,那么这个数就是偶数;否则就是奇数。
number = input("Enter a number, and I'll tell you if it's even or odd: ")number = int(number)if number % 2 == 0:        print("\nThe number " + str(number) + " is even.")else:        print("\nThe number " + str(number) + " is odd.")输出效果:Enter a number, and I'll tell you if it's even or odd: 42The number 42 is even.5、while 循环,for 循环用于针对聚集中的每个元素都一个代码块,而while 循环不绝地运行,直到指定的条件不满意为止。
current_number = 1while current_number <= 5:        print(current_number)        current_number += 1输出效果:123456、让用户选择何时退出,可使用while 循环让程序在用户乐意时不绝地运行,如下面的程序parrot.py所示。我们在此中界说了一个退出值,只要用户输入的不是这个值,程序就接着运行。
prompt = "\nTell me something, and I will repeat it back to you:"prompt += "\nEnter 'quit' to end the program. "message = ""while message != 'quit':          message = input(prompt)    print(message)输出效果:Tell me something, and I will repeat it back to you:Enter 'quit' to end the program. Hello everyone!Hello everyone!Tell me something, and I will repeat it back to you:Enter 'quit' to end the program. Hello again.Hello again.Tell me something, and I will repeat it back to you:Enter 'quit' to end the program. quitquit7、 使用标记,在要求许多条件都满意才继续运行的程序中,可界说一个变量,用于判断整个程序是否处于活动状态。这个变量被称为标记标记 ,充当了程序的交通讯号灯。你可让程序在标记为True 时继续运行,并在任何事故导致标记的值为False 时让程序克制运行。如许,在while 语句中就只需查抄一个条件——标记的当前值是否为True ,并将全部测试(是否发生了应将标记设置为False 的事故)都放在其他地方,从而让程序变得更为整洁。
prompt = "\nTell me something, and I will repeat it back to you:"  prompt += "\nEnter 'quit' to end the program. "active = Truewhile active:          message = input(prompt)    if message == 'quit':                  active = False    else:        print(message)输出效果:
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-18 20:33, Processed in 0.156848 second(s), 32 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表