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

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

yuyutoo 2025-02-19 14:27 5 浏览 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 泰山.

相关推荐

微软Win10/Win11版Copilot上线:支持OpenAI o3推理模型

IT之家4月3日消息,科技媒体WindowsLatest昨日(4月2日)发布博文,报道称Windows10、Windows11新版Copilot应用已摘掉Beta帽...

WinForm 双屏幕应用开发:原理、实现与优化

在当今的软件开发领域,多屏幕显示技术的应用越来越广泛。对于WinForm应用程序来说,能够支持双屏幕显示不仅可以提升用户体验,还能满足一些特定场景下的业务需求,比如在演示、监控或者多任务处理等场景...

推荐一个使用 C# 开发的 Windows10 磁贴美化小工具

...

OpenJDK 8 安装(openjdk 8 windows)

通常OpenJDK8和11都能互相编译和通用。我们建议使用11,但是如果你使用JDK8的话也是没有问题的。建议配置使用OpenJDK,不建议使用OracleJDK,主要是因为版...

基于 Linux 快速部署 OpenConnect VPN 服务(ocserv 实战指南)

一、前言在如今远程办公和内网穿透需求日益增长的背景下,搭建一套安全、稳定、高效的VPN系统显得尤为重要。OpenConnectServer(ocserv)是一个开源、高性能的VPN服务端软件...

巧妙设置让Edge浏览器更好用(edge怎么设置好用)

虽然现在新版本的Edge浏览器已经推出,但是毕竟还处于测试的状态中。而Win10系统里面自带的老版Edge浏览器,却越来越不被人重视。其实我们只需要根据实际情况对老版本的Edge浏览器进行一些简单的设...

WPF做一个漂亮的登录界面(wpf页面设计)

...

微软开源博客工具Open Live Writer更新:多项Bug修复

OpenLiveWriter前身是WindowsLiveWriter,是微软WindowsLive系列软件之一,曾经是博主们非常喜爱的一款所见即所得博文编辑工具,支持离线保存,还支持图像编辑...

基于OpenVINO的在线设计和虚拟试穿 | OPENAIGC大赛企业组优秀作品

在第二届拯救者杯OPENAIGC开发者大赛中,涌现出一批技术突出、创意卓越的作品。为了让这些优秀项目被更多人看到,我们特意开设了优秀作品报道专栏,旨在展示其独特之处和开发者的精彩故事。...

C#开源免费的Windows右键菜单管理工具

...

Windows10或11中隐藏的功能,用它再也不用担心电脑中病毒!

...

Python open函数详解(python open函数源码)

演示环境,操作系统:Win1021H2(64bit);Python解释器:3.8.10。open是Python的一个内置函数,一般用于本地文件的读写操作。用法如下。my_file=open(fi...

Windows 11 安装 Docker Desktop(Windows 11 安装助手 Windows 易升 关系)

...

Windows 11 新版发布:屏幕亮度自适应控制,小组件界面重新设计!

...

世界上最好用的Linux发行版之一,OpenSUSE安装及简单体验

背景之前无意在论坛里看到openSUSE的Linux发行版,被称为世界上最好用的Linux发行版之一(阔怕),一直想体验一下,于是这期做一个安装和简单体验教程吧。...

取消回复欢迎 发表评论: