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)

最新文章

  1. androud 自定义属性
  2. paip.批处理清理java项目冗余jar的方法
  3. snmp v3
  4. 初学java之异常类
  5. 简单几步优化技巧令你的Windows7系统加速
  6. UVALive - 6572 Shopping Malls floyd
  7. struts2与struts1整合,Unable to load configuration. - interceptor-ref ... struts.xml
  8. Linux“Bash”漏洞大爆发
  9. [SQL SERVER系列]存储过程,游标和触发器实例[原创]
  10. 遇到java.lang.OutOfMemoryError: Java heap space问题【持续跟踪中...】
  11. Nyoj 43 24 Point game 【DFS】
  12. 【转】多核CPU运行模式
  13. TopCoder SRM 625 Incrementing Sequence 题解
  14. Hadoop基本命令详解
  15. C#Winform实现自动更新
  16. 重启网络服务时 Bringing up interface eth0
  17. 阿里云各Linux发行版netcore兼容性评估报告---来自大石头的测试
  18. mysql学习笔记五 —— MHA
  19. Markdown 文件转化为work文档
  20. 实现泛型数组的冒泡排序算法 C#

热门文章

  1. Evensgn 剪树枝 树规
  2. BZOJ_2529_[Poi2011]Sticks_贪心
  3. [NOIP2002]字串变换 T2 双向BFS
  4. XMR挖矿教程
  5. eclipse使用javaFX写一个HelloWorkld
  6. Python + Appium 【已解决】driver(session)在多个class之间复用,执行完一个类的用例,再次执行下个类的用例时不需要初始化
  7. 【Maven篇】---解决Maven线上部署java.lang.ClassNotFoundException和no main manifest attribute解决方法
  8. 循环神经网络(Recurrent Neural Network,RNN)
  9. WebRTC系列(1)-手把手教你实现一个浏览器拍照室Demo
  10. C#净化版WebApi框架