Python chapter 7 learning notes

简介: 版权声明:本文为博主原创文章,原文均发表自http://www.yushuai.me。未经允许,禁止转载。 https://blog.csdn.net/davidcheungchina/article/details/78243686 函数input()工作原理n  示例演示了一种创建多行字符串的方式,第一行将消息的前半部分存储在变量prompt中,第二行,+=在存储在prompt中的字符串末尾附件一个字符串。
版权声明:本文为博主原创文章,原文均发表自http://www.yushuai.me。未经允许,禁止转载。 https://blog.csdn.net/davidcheungchina/article/details/78243686

函数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 + ".")

相关文章
|
22天前
|
机器学习/深度学习 算法 搜索推荐
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)
Machine Learning机器学习之决策树算法 Decision Tree(附Python代码)
|
4月前
|
Linux Apache Python
Python3 notes
Python3 notes
|
10月前
|
机器学习/深度学习 数据采集 数据挖掘
【Deep Learning 框架】Python中各类框架区别
当我们在学习深度学习的时候,往往会看到Sklearn、Pytorch、Tensorfloiw、Keras等各种各样的框架平台,看得我们眼花缭乱,这些框架平台究竟是干什么用的呢?🤔
94 0
|
11月前
|
安全 Python
Python3 notes
Python3 notes
|
11月前
|
Python
Python3 notes
Python3 notes
|
11月前
|
Python
Python3 notes
Python3 notes
|
11月前
|
Linux Python
Python3 notes
Python3 notes
|
12月前
|
Python
Python3 notes
Python3 notes
|
机器学习/深度学习 算法 Python
【莫烦Python强化学习笔记】Q Learning
【莫烦Python强化学习笔记】Q Learning
|
索引 Python