文档目录:

一、if语句
二、检索条件
三、用户输入input
四、while+inoput(),让用户选择何时退出
五、break与continue
六、while循环处理字典和列表

---------------------------------------分割线:正文--------------------------------------------------------

一、if语句

1、if-else语句

  1. cars=['audi','bmw','toyota']
  2. for car in cars:
  3. if car=='bmw':
  4. print(car.upper())
  5. else:
  6. print(car.title())

查看结果:

  1. Audi
  2. BMW
  3. Toyota

2、if-elif-else

  1. age=12
  2. if(age<4):
  3. print("Your admission cost is $0.")
  4. elif(age<18):
  5. print("Your admission cost is $25.")
  6. else:
  7. print("Your admission cost is $40.")

查看结果:

  1. Your admission cost is $25.

二、检索条件

1、忽略大小写

  1. car='Audi'
  2. print(car=='audi')
  3. print(car.lower()=='audi')
  4. print(car.upper()=='AUDI')

查看运行结果:

2、检查不相等

  1. car='Audi'
  2. print(car !='AUDI')

查看运行结果:

  1. True

3、检查多个条件

  1. age_0=22
  2. age_1=18
  3. print(age_0>=21 and age_1>=21)
  4. print((age_0>=21) and (age_1<21))
  5. print(age_0>=21 or age_1>=21)

查看运行结果:

False
True
True

4、检查特定值是否包含在列表中

  1. testList=['A','B','C']
  2. print('A' in testList)
  3. print('D' not in testList)

查看运行结果:

  1. True
  2. True

5、检查列表是否为空

  1. testList2=[1,2,3]
  2. testList3=[]
  3. if testList2:
  4. for test in testList2:
  5. print(test)
  6. else:
  7. print("testList2为空")
  8. if testList3:
  9. for test in testList2:
  10. print(test)
  11. else:
  12. print("testList3为空")

查看运行结果:

  1. 1
  2. 2
  3. 3
  4. testList2为空

三、用户输入input

1、用户输入并返回

  1. message=input("Tell me something,and I will repeat it back to you:")
  2. print(message)

查看结果:

  1. Tell me something,and I will repeat it back to you:hello world
  2. hello world

2、f表达式返回

  1. name=input("Please enter yout name:")
  2. print(f"hello,{name}!")

查看结果:

  1. Please enter yout name:jack
  2. hello,jack!

3、更长的句子

  1. prompt="Tell me something,and I will repeat it back to you,"
  2. prompt+="\nWhat's your name?\n"
  3. name=input(prompt)
  4. print(f"hello {name.title()}")

查看结果:

  1. Tell me something,and I will repeat it back to you,
  2. What's your name?
  3. mary
  4. hello Mary

4、int()获取数值输入

  1. age=input("How old are you?:")
  2. age=int(age)
  3. print(f"Your age is {age}!")

查看结果:

  1. How old are you?:27
  2. Your age is 27!

四、while+inoput(),让用户选择何时退出

1、普通用法

  1. prompt="Tell me something,and I will repeat it back to you:"
  2. prompt+="\nEnter 'quit' to be end the program."
  3. message=""
  4. while message!='quit':
  5. message=input(prompt)
  6. if message!='quit':
  7. print(message)

查看结果

  1. Tell me something,and I will repeat it back to you:
  2. Enter 'quit' to be end the program.hello world
  3. hello world
  4. Tell me something,and I will repeat it back to you:
  5. Enter 'quit' to be end the program.quit

2、进阶用法

  1. prompt="Tell me something,and I will repeat it back to you:"
  2. prompt+="\nEnter 'quit' to be end the program."
  3. #设置标志
  4. active=True
  5. while active:
  6. message=input(prompt)
  7. if message=='quit':
  8. active=False
  9. else:
  10. print(message)

查看结果:

  1. Tell me something,and I will repeat it back to you:
  2. Enter 'quit' to be end the program.ok
  3. ok
  4. Tell me something,and I will repeat it back to you:
  5. Enter 'quit' to be end the program.quit

五、break与continue

1、break:退出循环

  1. prompt="Tell me a city you want got to."
  2. prompt+="\nEnter 'quit' to be end the program:"
  3. while True:
  4. city=input(prompt)
  5. if city=='quit':
  6. break
  7. else:
  8. print(f"You love to go to {city.title()}!")

查看结果:

  1. Tell me a city you want got to.
  2. Enter 'quit' to be end the program:nanjing
  3. You love to go to Nanjing!
  4. Tell me a city you want got to.
  5. Enter 'quit' to be end the program:newyork
  6. You love to go to Newyork!
  7. Tell me a city you want got to.
  8. Enter 'quit' to be end the program:quit

2、continue:跳出本次循环,继续执行

  1. current_number=0
  2. while current_number<10:
  3. current_number+=1
  4. if current_number%2==0:
  5. continue
  6. else:
  7. print(current_number)

查看结果:

  1. 1
  2. 3
  3. 5
  4. 7
  5. 9

六、while循环处理字典和列表

1、while+列表:用户认证

  1. #处理用户认证的列表
  2. unconfirmed_users=['alice','brian','candace']
  3. confirmed_users=[]
  4. while unconfirmed_users:
  5. current_user=unconfirmed_users.pop()
  6. print(f"Verfying users:{current_user.title()}")
  7. confirmed_users.append(current_user)
  8. #显示所有验证的用户
  9. print("\nThe following users have been confirmed!")
  10. for confirm_user in confirmed_users:
  11. print(confirm_user.title())

查看结果

  1. Verfying users:Candace
  2. Verfying users:Brian
  3. Verfying users:Alice
  4.  
  5. The following users have been confirmed!
  6. Candace
  7. Brian
  8. Alice

2、删除特定值的所有列表元素

  1. pet=['dog','cat','pig','cat','triger','rabbit']
  2. print(f"删除前:{pet}")
  3. while 'cat' in pet:
  4. pet.remove('cat')
  5. print(f"删除后:{pet}")

查看结果:

  1. 删除前:['dog', 'cat', 'pig', 'cat', 'triger', 'rabbit']
  2. 删除后:['dog', 'pig', 'triger', 'rabbit']

3、使用字典记录问卷调差

  1. mydict={}
  2. active=True
  3. while active:
  4. name=input("Please enter your name:")
  5. city=input("Please enter what city you want go to:")
  6. mydict[name]=city
  7. next=input("Do you want to send this questionnaire to another people(yes/no):")
  8. if next=='no':
  9. break
  10. print("The questionnaire is over,now the result is:")
  11. for name,city in mydict.items():
  12. print(f"{name.title()} want go to {city.title()}!")

查看结果:

  1. Please enter your name:lily
  2. Please enter what city you want go to:nanjing
  3. Do you want to send this questionnaire to another people(yes/no):yes
  4. Please enter your name:mary
  5. Please enter what city you want go to:shanghai
  6. Do you want to send this questionnaire to another people(yes/no):yes
  7. Please enter your name:tom
  8. Please enter what city you want go to:london
  9. Do you want to send this questionnaire to another people(yes/no):no
  10. The questionnaire is over,now the result is:
  11. Lily want go to Nanjing!
  12. Mary want go to Shanghai!
  13. Tom want go to London!

python进阶(3)--条件判断、用户输入的更多相关文章

  1. python入门学习:6.用户输入和while循环

    python入门学习:6.用户输入和while循环 关键点:输入.while循环 6.1 函数input()工作原理6.2 while循环简介6.3 使用while循环处理字典和列表 6.1 函数in ...

  2. Python基础:条件判断与循环的两个要点

    一.条件判断: Python中,条件判断用if语句实现,多个条件判断时用if...elif实现:看下面一段程序 #python 3.3.5 #test if...elif age = 20 if ag ...

  3. JavaScript 判断用户输入的邮箱及手机格式是否正确

    JavaScript判断用户输入的邮箱格式是否正确.判断用户输入的手机号格式是否正确,下面有个不错的示例,感兴趣的朋友可以参考下. 复制代码代码如下: /*  * 功能:判断用户输入的邮箱格式是否正确 ...

  4. java判断用户输入的是否至少含有N位小数

    判断用户输入的是否至少含有N位小数. 1.当用户输入的是非数字时抛出异常,返回false. 2.当用户输入数字是,判断其数字是否至少含有N位小数,如果不含有,返回false. 3.当用户输入的数字的小 ...

  5. 判断用户输入YES或NO

    #!bin/bash#作者:liusingbon#功能:判断用户输入的是 Yes 或 NOread -p "Are you sure?[y/n]:" surecase $sure ...

  6. python学习第六天 条件判断和循环

    总归来讲,学过C语言的同学,对条件判断和循环并不陌生.这次随笔只是普及一下python的条件判断和循环对应的语法而已. 条件判断: 不多说,直接贴代码: age = 23 if age >= 6 ...

  7. python基础知识--条件判断和循环

    一.输入输出 python怎么来接收用户输入呢,使用input函数,python2中使用raw_input,接收的是一个字符串,输出呢,第一个程序已经写的使用print,代码入下: 1 name=in ...

  8. Python学习笔记—条件判断和循环

    条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= ...

  9. python学习:注释、获取用户输入、字符串拼接、运算符、表达式

    注释 #为单行注释'''三个单引号(或者"""三个双引号)为多行注释,例如'''被注释的内容''' '''三个单引号还可以起到多行打印的功能. #ctrl+? 选中的多行 ...

随机推荐

  1. Codeforces 1485F Copy or Prefix Sum

    题目链接 点我跳转 题目大意 给定一个长度为 \(N\) 的序列 \(bi\) 问有多少个长度为 \(N\) 的序列 \(a\) 使得 \(b[i] = a[i]\) 或 \(b[i] = ∑a[j] ...

  2. Spring 中的 MetaData 接口

    什么是元数据(MetaData) 先直接贴一个英文解释: Metadata is simply data about data. It means it is a description and co ...

  3. m1款MacBook Air 使用3个月总结及原生运行于apple架构软件推荐

    前言 我之前一直是一个坚定的Windows/Android党,大学的时候用过几台iPhone,感觉也就那样.这次m1版本的Mac一发布我直接又转回apple阵营了,11月份的时候官网订了一台m1 版本 ...

  4. JUC并发编程学习笔记

    JUC并发编程学习笔记 狂神JUC并发编程 总的来说还可以,学到一些新知识,但很多是学过的了,深入的部分不多. 线程与进程 进程:一个程序,程序的集合,比如一个音乐播发器,QQ程序等.一个进程往往包含 ...

  5. CSS实现页面切换时的滑动效果

    最近在开发手机端APP页面功能时遇到一个需求:某个页面查询的数据有三种分类,需要展示在同一页面上,用户通过点击分类标签来查看不同类型的数据, 期望效果是 用户点击标签切换时另一个页面能够以一个平滑切入 ...

  6. Vue框架简介及简单使用

    目录 一.前端框架介绍 二.vue框架简介 三.vue使用初体验 1. vue如何在页面中引入 2. 插值表达式 3. 文本指令 4. 方法指令(事件指令) 5. 属性指令 四.js数据类型补充 1. ...

  7. 报错: You are using pip version 10.0.1, however version 18.0 is available.

    报错: You are using pip version 10.0.1, however version 18.0 is available. You should consider upgradi ...

  8. AXU2CGB开发板验证Vitis加速基本平台创建

    Vitis 加速基本平台创建 1.Vivado 工程创建,硬件平台bd 图如下所示 1.1.双击Block图中ZYNQ核,配置相关参数 1.1.1.Low Speed 配置,在 I/O Configu ...

  9. [源码解析] 消息队列 Kombu 之 基本架构

    [源码解析] 消息队列 Kombu 之 基本架构 目录 [源码解析] 消息队列 Kombu 之 基本架构 0x00 摘要 0x01 AMQP 1.1 基本概念 1.2 工作过程 0x02 Poll系列 ...

  10. 翻译:《实用的Python编程》03_04_Modules

    目录 | 上一节 (3.3 错误检查) | 下一节 (3.5 主模块) 3.4 模块 本节介绍模块的概念以及如何使用跨多个文件的函数. 模块和导入 任何一个 Python 源文件都是一个模块. # f ...