Python编程从入门到实践笔记——用户输入和while循环

#coding=utf-8
#函数input()让程序暂停运行,等待用户输入一些文本。得到用户的输入以后将其存储在一个变量中,方便后续使用
name=input("Please Enter Your Name:")
print("Hello!"+name+"!Welcome to Python world!") prompt = "If you tell us who you are, we can personalize the messages you see.\nWhat is your first name:"
name=input(prompt)
print("Hello!"+name+"!") #将数字的字符串表示转换为数值 int()
age=input("How old are you?")
age=int(age)
if age < 18:
print("Deny")
elif age >= 18 and age <= 60:
print("Access")
else:
print("Sorry") #求模运算符 % 返回余数 #while循环
current_number = 1
while current_number <= 5:
print("current_number:"+str(current_number))
current_number += 1;#注意python中没有++操作,究其原因,python中变量是以内容为基准而不是像 c 中以变量名为基准 #使用标志
active=True
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print(massage) #使用break退出循环
while True:
message = input(prompt)
if message == 'quit':
break
else:
print(massage) #使用continue 和其他语言的break、continue用法都一样
#避免无限循环,也就是说要注意循环的条件
#如果陷入了无限循环,可以按Ctrl+C,与Linux中命令一样 #使用while循环来出列列表和字典
#在列表之间移动元素
unconfirmed_users=['alice','bob','candy']
confirmed_users=[]
while unconfirmed_users:
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.title()) #删除包含特定值的所有列表元素
#remove()删除列表中特定值只删除第一个匹配的,无法删除多个;如果想全部删除,通过遍历来删除
pets=['dog','cat','panda','fish','rabbit','cat']
print(pets)
while 'cat' in pets:
pets.remove('cat') print(pets) #使用用户输入来填充字典
responses = {}
polling_active = True
while polling_active :
name = input("Name:")
response = input("Response:") responses[name] = response repeat = input("yes or no:")
if repeat == 'no':
polling_active = False print(responses)

Python编程从入门到实践笔记——用户输入和while循环的更多相关文章

  1. Python编程从入门到实践笔记——异常和存储数据

    Python编程从入门到实践笔记——异常和存储数据 #coding=gbk #Python编程从入门到实践笔记——异常和存储数据 #10.3异常 #Python使用被称为异常的特殊对象来管理程序执行期 ...

  2. Python编程从入门到实践笔记——函数

    Python编程从入门到实践笔记——函数 #coding=gbk #Python编程从入门到实践笔记——函数 #8.1定义函数 def 函数名(形参): # [缩进]注释+函数体 #1.向函数传递信息 ...

  3. Python编程从入门到实践笔记——文件

    Python编程从入门到实践笔记——文件 #coding=gbk #Python编程从入门到实践笔记——文件 #10.1从文件中读取数据 #1.读取整个文件 file_name = 'pi_digit ...

  4. Python编程从入门到实践笔记——类

    Python编程从入门到实践笔记——类 #coding=gbk #Python编程从入门到实践笔记——类 #9.1创建和使用类 #1.创建Dog类 class Dog():#类名首字母大写 " ...

  5. Python编程从入门到实践笔记——字典

    Python编程从入门到实践笔记——字典 #coding=utf-8 #字典--放在{}中的键值对:跟json很像 #键和值之间用:分隔:键值对之间用,分隔 alien_0 = {'color':'g ...

  6. Python编程从入门到实践笔记——if语句

    Python编程从入门到实践笔记——if语句 #coding=utf-8 cars=['bwm','audi','toyota','subaru','maserati'] bicycles = [&q ...

  7. Python编程从入门到实践笔记——操作列表

    Python编程从入门到实践笔记——操作列表 #coding=utf-8 magicians = ['alice','david','carolina'] #遍历整个列表 for magician i ...

  8. Python编程从入门到实践笔记——列表简介

    Python编程从入门到实践笔记——列表简介 #coding=utf-8 #列表——我的理解等于C语言和Java中的数组 bicycles = ["trek","cann ...

  9. Python编程从入门到实践笔记——变量和简单数据类型

    Python编程从入门到实践笔记——变量和简单数据类型 #coding=gbk #变量 message_1 = 'aAa fff' message_2 = 'hart' message_3 = &qu ...

随机推荐

  1. iOS 支付(含支付宝、银联、微信)

    资料 支付宝 //文档idk都包含了安卓.iOS版 银 联 银联官网资料 Demo Demo给了一个订单号,做测试使用,若出现支付失败什么的,可能是已经被别人给支付了,或者是服务器订单过期了 ~ 一. ...

  2. zabbix监控elasticsearch

    1.环境概述 虚拟机系统:CentOS Linux release 7.3.1611 (Core) 宿主机系统:Mac Sierra version 10.12.3 nginx:1.10.3 php: ...

  3. Windows上安装配置SSH教程(4)——WinSCP+OpenSSH 使用公钥自动登陆

    -------------------- 知识点汇总:http://www.cnblogs.com/feipeng8848/p/8559803.html -------------------- 重要 ...

  4. dotnet-warp && NSSM 部署 .net core 项目到 windows 服务

    如果你想将 .net core 项目以服务的形式部署到 windows 系统,希望本篇文章能够让你少走弯路 dotnet-warp 安装使用 dotnet-warp 是一个全局的.NET Core 工 ...

  5. Node中流的概念

    在学习node的过程中,对于流的概念一直不是很理解,通过查阅一些资料,现在将自己对流的一些理解进行总结一下. 一.流的理解 首先我们必须知道什么是流,很多书中只是提到使用流读写文件怎么怎么方便,却不提 ...

  6. 图解Java线程的生命周期,看完再也不怕面试官问了

    文章首发自个人微信公众号: 小哈学Java https://www.exception.site/java-concurrency/java-concurrency-thread-life-cycle ...

  7. 使用强类型实体Id来避免原始类型困扰(一)

    原文地址:https://andrewlock.net/using-strongly-typed-entity-ids-to-avoid-primitive-obsession-part-1/ 作者: ...

  8. ES 13 - Elasticsearch的元字段 (_index、_type、_source、_routing等)

    目录 1 标识元字段 1.1 _index - 文档所属的索引 1.2 _uid - 包含_type和_id的复合字段 1.3 _type - 文档的类型 1.4 _id - 文档的id 2 文档来源 ...

  9. 工厂模式讲解, 引入Spring IOC

    目录 引入 简单工厂 抽象工厂 Spring的bean工厂 模拟Spring工厂实现 模拟IOC 引入 假设有一个司机, 需要到某个城市, 于是我们给他一辆汽车 public class Demo { ...

  10. Scrapy爬虫遇到 ‘Forbidden by robots.txt’的问题

    今天在爬知乎精华时,出现了‘Forbidden by robots.txt’的问题 了解到到scrapy在爬取设定的url之前,它会先向服务器根目录请求一个txt文件,这个文件规定了爬取范围 scra ...