Python chapter 7 learning notes
来源:互联网 发布:五线胆码的算法 编辑:程序博客网 时间:2024/05/22 10:44
函数input()工作原理
n 示例演示了一种创建多行字符串的方式,第一行将消息的前半部分存储在变量prompt中,第二行,+=在存储在prompt中的字符串末尾附件一个字符串。
例如:
prompt = "If you tell us who you are, we can personalize the messages you see."
prompt +="\nWhat is your first name?\n"
n 在使用时如果输入数字,系统将自动将其转换为字符串。如果想仍作为数字使用,可以将其强制转换为数字,例如整数可以用int()。
l while循环
n 在循环中使用continue,可以继续执行循环,它不会像break语句那样不再执行余下的代码并退出整个循环,而是重新开始这个循环。
n 避免无限循环:如果程序陷入无限循环,可按Ctrl+C,也可以关闭显示程序输出的终端窗口(不适用于Sublime Text)
l 使用while循环来处理列表和字典
n 在列表之间移动元素
n 删除包含特定值的所有列表元素
n 使用用户输入来填充字典
# -*- coding: utf-8 -*-
responses = {}
polling_active = True
while polling_active:
name = input("\nWhat is your name?")
response = input("Which mountain would you like to climb someday? ")
responses[name] = response#直接字典赋值方法,别忘了
repeat = input("Would you like to let another person respod?(yes/no)")
if repeat == 'no':
polling_active = False
print("\n— POLL RESULTS —")
for name,response in responses.items():
print(name + " would like to climb " + response + ".")
- Python chapter 7 learning notes
- Python chapter 4 learning notes
- Python chapter 5 learning notes
- Python chapter 6 learning notes
- Python chapter 8 learning notes
- Python chapter 2&3 learning notes
- Python-learning-SMACH-Notes
- Assignments-Learning Python Chapter 11
- Learning Python-the third chapter
- Head First Python Notes - Chapter 5
- Head First Python Notes - Chapter 6
- Notes of Python Learning 2/10/2016
- Expressions, and Prints-Learning Python-Chapter 11
- Gray Hat --Learning Python Chapter 1
- Gray Hat --Learning Python Chapter 2
- Learning Notes
- Python Notes (7)
- Python Algorithms Learning Notes(1)--Asymptotic Notations
- C++中String类型的逆序
- Python chapter 6 learning notes
- 1.GDI+ 常用方法详解
- 直接排序算法
- 第 0004 题: 任一个英文的纯文本文件,统计其中的单词出现的个数
- Python chapter 7 learning notes
- SQL语句范例之索引和PLSQL
- Java:Collections框架
- XlistView
- [成长] 生病~
- LuoguP1720 月落乌啼算钱 解题报告【模拟+找规律/数学】
- 几种Java 枚举查找且不抛异常的实现及其优劣比较
- 浅谈nginx内存池(二)
- drawerlayout_demo