跳至正文
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
小奥的学习笔记

万丈高楼平地起,勿在浮沙筑高台

小奥的学习笔记

万丈高楼平地起,勿在浮沙筑高台

  • 首页
  • Sample Page
  • 隐私策略
  • GuestBook
  • 慕课课程(MOOC’s Lessons)
  • Footprints
  • About Me
  • 个人履历
  • 首页
  • Sample Page
  • 隐私策略
  • GuestBook
  • 慕课课程(MOOC’s Lessons)
  • Footprints
  • About Me
  • 个人履历
关

搜索

  • https://www.facebook.com/
  • https://twitter.com/
  • https://t.me/
  • https://www.instagram.com/
  • https://youtube.com/
Subscribe
Python

Python chapter 7 learning notes

作者 yszhang
2017年10月15日 1 分钟阅读
0

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

标签:

Pythonpython学习
作者

yszhang

关注我
其他文章
上一个

Python chapter 6 learning notes

下一个

安装package control的几种方法

暂无评论!成为第一个。

发表回复 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Copyright 2026 — 小奥的学习笔记. All rights reserved. Blogsy WordPress Theme