百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程网 > 正文

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

yuyutoo 2025-02-19 14:27 1 浏览 0 评论

Python学习笔记-基础语法

第七章 用户输入和while循环

  • 一、input()函数
  • 二、while循环
  • 三、使用while循环来处理列表和字典

一、input()函数

1、int()函数:

  • input()函数输出为字符串,使用int()将之转换为数值
age = input("请输入你的年龄:")
print(age >= 19)

输出结果为:
请输入你的年龄:11
Traceback (most recent call last):
 File "D:\Python_Work\Python学习\Python编程-从入门到实践\练习.py", line 2, in 
  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
  • 在任何Python循环中都可以使用break语句
  • 可使用break语句来退出遍历列表或字典的for循环

3、在循环中使用continue

  • continue:回到循环开头,根据条件测试结果决定是否继续执行循环
  • 它不像break语句那样不再执行余下的代码并推出整个循环
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循环来处理列表和字典

  • for循环中不应修改列表,否则将导致Python难以跟踪其中的元素
  • 要在遍历列表的同时对其进行修改,可使用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 泰山.

相关推荐

如何在HTML中使用JavaScript:从基础到高级的全面指南!

“这里是云端源想IT,帮你...

推荐9个Github上热门的CSS开源框架

大家好,我是Echa。...

前端基础知识之“CSS是什么?”_前端css js

...

硬核!知网首篇被引过万的论文讲了啥?作者什么来头?

整理|袁小华近日,知网首篇被引量破万的中文论文及其作者备受关注。知网中心网站数据显示,截至2021年7月23日,由华南师范大学教授温忠麟等人发表在《心理学报》2004年05期上的学术论文“中介效应检验...

为什么我推荐使用JSX开发Vue3_为什么用vue不用jquery

在很长的一段时间中,Vue官方都以简单上手作为其推广的重点。这确实给Vue带来了非常大的用户量,尤其是最追求需求开发效率,往往不那么在意工程代码质量的国内中小企业中,Vue占据的份额极速增长...

【干货】一文详解html和css,前端开发需要哪些技术?
【干货】一文详解html和css,前端开发需要哪些技术?

网站开发简介...

2025-02-20 18:34 yuyutoo

分享几个css实用技巧_cssli

本篇将介绍几个css小技巧,目录如下:自定义引用标签的符号重置所有标签样式...

如何在浏览器中运行 .NET_怎么用浏览器运行代码

概述:...

前端-干货分享:更牛逼的CSS管理方法-层(CSS Layers)

使用CSS最困难的部分之一是处理CSS的权重值,它可以决定到底哪条规则会最终被应用,尤其是如果你想在Bootstrap这样的框架中覆盖其已有样式,更加显得麻烦。不过随着CSS层的引入,这一...

HTML 基础标签库_html标签基本结构
HTML 基础标签库_html标签基本结构

HTML标题HTML标题(Heading)是通过-...

2025-02-20 18:34 yuyutoo

前端css面试20道常见考题_高级前端css面试题

1.请解释一下CSS3的flexbox(弹性盒布局模型),以及适用场景?display:flex;在父元素设置,子元素受弹性盒影响,默认排成一行,如果超出一行,按比例压缩flex:1;子元素设置...

vue引入外部js文件并使用_vue3 引入外部js

要在Vue中引入外部的JavaScript文件,可以使用以下几种方法:1.使用``标签引入外部的JavaScript文件。在Vue的HTML模板中,可以直接使用``标签来引入外部的JavaScrip...

网页设计得懂css的规范_html+css网页设计

在初级的前端工作人员,刚入职的时候,可能在学习前端技术,写代码不是否那么的规范,而在工作中,命名的规范的尤为重要,它直接与你的代码质量挂钩。网上也受很多,但比较杂乱,在加上每年的命名都会发生一变化。...

Google在Chrome中引入HTML 5.1标记

虽然负责制定Web标准的WorldWideWebConsortium(W3C)尚未宣布HTML5正式推荐规格,而Google已经迁移到了HTML5.1。即将发布的Chrome38将引入H...

HTML DOM 引用( ) 对象_html中如何引用js

引用对象引用对象定义了一个同内联元素的HTML引用。标签定义短的引用。元素经常在引用的内容周围添加引号。HTML文档中的每一个标签,都会创建一个引用对象。...

取消回复欢迎 发表评论: