python进阶(3)--条件判断、用户输入
文档目录:
一、if语句
二、检索条件
三、用户输入input
四、while+inoput(),让用户选择何时退出
五、break与continue
六、while循环处理字典和列表
---------------------------------------分割线:正文--------------------------------------------------------
一、if语句
1、if-else语句
cars=['audi','bmw','toyota']
for car in cars:
if car=='bmw':
print(car.upper())
else:
print(car.title())
查看结果:
Audi
BMW
Toyota
2、if-elif-else
age=12
if(age<4):
print("Your admission cost is $0.")
elif(age<18):
print("Your admission cost is $25.")
else:
print("Your admission cost is $40.")
查看结果:
Your admission cost is $25.
二、检索条件
1、忽略大小写
car='Audi'
print(car=='audi')
print(car.lower()=='audi')
print(car.upper()=='AUDI')
查看运行结果:
2、检查不相等
car='Audi'
print(car !='AUDI')
查看运行结果:
True
3、检查多个条件
age_0=22
age_1=18
print(age_0>=21 and age_1>=21)
print((age_0>=21) and (age_1<21))
print(age_0>=21 or age_1>=21)
查看运行结果:
False
True
True
4、检查特定值是否包含在列表中
testList=['A','B','C']
print('A' in testList)
print('D' not in testList)
查看运行结果:
True
True
5、检查列表是否为空
testList2=[1,2,3]
testList3=[]
if testList2:
for test in testList2:
print(test)
else:
print("testList2为空")
if testList3:
for test in testList2:
print(test)
else:
print("testList3为空")
查看运行结果:
1
2
3
testList2为空
三、用户输入input
1、用户输入并返回
message=input("Tell me something,and I will repeat it back to you:")
print(message)
查看结果:
Tell me something,and I will repeat it back to you:hello world
hello world
2、f表达式返回
name=input("Please enter yout name:")
print(f"hello,{name}!")
查看结果:
Please enter yout name:jack
hello,jack!
3、更长的句子
prompt="Tell me something,and I will repeat it back to you,"
prompt+="\nWhat's your name?\n"
name=input(prompt)
print(f"hello {name.title()}")
查看结果:
Tell me something,and I will repeat it back to you,
What's your name?
mary
hello Mary
4、int()获取数值输入
age=input("How old are you?:")
age=int(age)
print(f"Your age is {age}!")
查看结果:
How old are you?:27
Your age is 27!
四、while+inoput(),让用户选择何时退出
1、普通用法
prompt="Tell me something,and I will repeat it back to you:"
prompt+="\nEnter 'quit' to be end the program."
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 be end the program.hello world
hello world
Tell me something,and I will repeat it back to you:
Enter 'quit' to be end the program.quit
2、进阶用法
prompt="Tell me something,and I will repeat it back to you:"
prompt+="\nEnter 'quit' to be end the program."
#设置标志
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 be end the program.ok
ok
Tell me something,and I will repeat it back to you:
Enter 'quit' to be end the program.quit
五、break与continue
1、break:退出循环
prompt="Tell me a city you want got to."
prompt+="\nEnter 'quit' to be end the program:"
while True:
city=input(prompt)
if city=='quit':
break
else:
print(f"You love to go to {city.title()}!")
查看结果:
Tell me a city you want got to.
Enter 'quit' to be end the program:nanjing
You love to go to Nanjing!
Tell me a city you want got to.
Enter 'quit' to be end the program:newyork
You love to go to Newyork!
Tell me a city you want got to.
Enter 'quit' to be end the program:quit
2、continue:跳出本次循环,继续执行
current_number=0
while current_number<10:
current_number+=1
if current_number%2==0:
continue
else:
print(current_number)
查看结果:
1
3
5
7
9
六、while循环处理字典和列表
1、while+列表:用户认证
#处理用户认证的列表
unconfirmed_users=['alice','brian','candace']
confirmed_users=[]
while unconfirmed_users:
current_user=unconfirmed_users.pop()
print(f"Verfying users:{current_user.title()}")
confirmed_users.append(current_user)
#显示所有验证的用户
print("\nThe following users have been confirmed!")
for confirm_user in confirmed_users:
print(confirm_user.title())
查看结果
Verfying users:Candace
Verfying users:Brian
Verfying users:Alice The following users have been confirmed!
Candace
Brian
Alice
2、删除特定值的所有列表元素
pet=['dog','cat','pig','cat','triger','rabbit']
print(f"删除前:{pet}")
while 'cat' in pet:
pet.remove('cat')
print(f"删除后:{pet}")
查看结果:
删除前:['dog', 'cat', 'pig', 'cat', 'triger', 'rabbit']
删除后:['dog', 'pig', 'triger', 'rabbit']
3、使用字典记录问卷调差
mydict={}
active=True
while active:
name=input("Please enter your name:")
city=input("Please enter what city you want go to:")
mydict[name]=city
next=input("Do you want to send this questionnaire to another people(yes/no):")
if next=='no':
break
print("The questionnaire is over,now the result is:")
for name,city in mydict.items():
print(f"{name.title()} want go to {city.title()}!")
查看结果:
Please enter your name:lily
Please enter what city you want go to:nanjing
Do you want to send this questionnaire to another people(yes/no):yes
Please enter your name:mary
Please enter what city you want go to:shanghai
Do you want to send this questionnaire to another people(yes/no):yes
Please enter your name:tom
Please enter what city you want go to:london
Do you want to send this questionnaire to another people(yes/no):no
The questionnaire is over,now the result is:
Lily want go to Nanjing!
Mary want go to Shanghai!
Tom want go to London!
python进阶(3)--条件判断、用户输入的更多相关文章
- python入门学习:6.用户输入和while循环
python入门学习:6.用户输入和while循环 关键点:输入.while循环 6.1 函数input()工作原理6.2 while循环简介6.3 使用while循环处理字典和列表 6.1 函数in ...
- Python基础:条件判断与循环的两个要点
一.条件判断: Python中,条件判断用if语句实现,多个条件判断时用if...elif实现:看下面一段程序 #python 3.3.5 #test if...elif age = 20 if ag ...
- JavaScript 判断用户输入的邮箱及手机格式是否正确
JavaScript判断用户输入的邮箱格式是否正确.判断用户输入的手机号格式是否正确,下面有个不错的示例,感兴趣的朋友可以参考下. 复制代码代码如下: /* * 功能:判断用户输入的邮箱格式是否正确 ...
- java判断用户输入的是否至少含有N位小数
判断用户输入的是否至少含有N位小数. 1.当用户输入的是非数字时抛出异常,返回false. 2.当用户输入数字是,判断其数字是否至少含有N位小数,如果不含有,返回false. 3.当用户输入的数字的小 ...
- 判断用户输入YES或NO
#!bin/bash#作者:liusingbon#功能:判断用户输入的是 Yes 或 NOread -p "Are you sure?[y/n]:" surecase $sure ...
- python学习第六天 条件判断和循环
总归来讲,学过C语言的同学,对条件判断和循环并不陌生.这次随笔只是普及一下python的条件判断和循环对应的语法而已. 条件判断: 不多说,直接贴代码: age = 23 if age >= 6 ...
- python基础知识--条件判断和循环
一.输入输出 python怎么来接收用户输入呢,使用input函数,python2中使用raw_input,接收的是一个字符串,输出呢,第一个程序已经写的使用print,代码入下: 1 name=in ...
- Python学习笔记—条件判断和循环
条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= ...
- python学习:注释、获取用户输入、字符串拼接、运算符、表达式
注释 #为单行注释'''三个单引号(或者"""三个双引号)为多行注释,例如'''被注释的内容''' '''三个单引号还可以起到多行打印的功能. #ctrl+? 选中的多行 ...
随机推荐
- NGK DeFi项目即将上线,打造去中心化闭环金融生态!
据最新官方消息称:NGK已于近日宣布将进军DeFi领域,NGK此次的DeFi的项目将会是一个去中心的交易平台,其最大的功能是进行数字货币的交换.在用户选择了需要支付的数字货币和想购买的数字货币后,系统 ...
- java的read方法
public class RandomAccessDemo6 { public static void main(String[] args) throws IOException { RandomA ...
- 鸿蒙开源第三方组件——进度轮ProgressWheel
目录:1.前言2.背景3.组件功能展示4.Sample解析5.Library解析6.作者系列文章合集 前言 基于安卓平台的进度轮组件ProgressWheel(https://github.com/A ...
- Bitter.NotifyOpenPaltform : HTTP 异步消息接收调度中心--开源贡献 之 一:简介
现在互联网的系统越来越趋向于复杂,从单体系统到现在的微服务体系演变.公司与公司的分工也越来越明确. 大数据公司提供了大数据服务 人脸识别公司提供了人脸识别服务 OCR 公司提供了专业的OCR 服务 车 ...
- Python切换版本工具pyenv
目录 安装pyenv 安装与查看py版本 切换py版本 结合ide使用示例 和virtualenv的一些区别 参考文献 使用了一段时间,我发现这玩意根本不是什么神器,简直就是垃圾,安装多版本总是失败, ...
- Latency 和 Delay 区别
时延:Latency 指的是一个报文进入一台设备以致这台设备所经历的时间.实际上考验的是报文在这台设备上消耗的时间.时间越短,这台设备的性能越高. 延时:Delay 是指一个操作和另个一个操作之间 ...
- 不使用map和set实现LRU——那用List?
遇到一道面试题,不使用map和set实现LRU,要求get的时间复杂度为O(logn),put的时间复杂度不超过O(n).想到了用ArrayList来实现,保存有序的key.然而牵涉add节点,在保证 ...
- 微信小程序日期时间选择器(精确到秒)
<picker mode="multiSelector" value="{{dateTime1}}" bindchange="changeDat ...
- 在scanf函数中占位符使用错误而产生的一些错误
出现的问题 在做编程题的的时候,遇到了一个很奇怪的错误,出问题的代码如下: 1 #include <cstdio> 2 using namespace std; 3 4 int main( ...
- python 操作符** (两个乘号就是乘方)
一个乘号*,如果操作数是两个数字,就是这两个数字相乘,如2*4,结果为8**两个乘号就是乘方.比如3**4,结果就是3的4次方,结果是81 *如果是字符串.列表.元组与一个整数N相乘,返回一个其所有元 ...