Python编程从入门到实践笔记——用户输入和while循环
发布日期:2021-05-09 06:23:05
浏览次数:20
分类:博客文章
本文共 1864 字,大约阅读时间需要 6 分钟。
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 = 1while current_number <= 5: print("current_number:"+str(current_number)) current_number += 1;#注意python中没有++操作,究其原因,python中变量是以内容为基准而不是像 c 中以变量名为基准 #使用标志active=Truewhile 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 = Truewhile polling_active : name = input("Name:") response = input("Response:") responses[name] = response repeat = input("yes or no:") if repeat == 'no': polling_active = False print(responses)
转载地址:https://www.cnblogs.com/sgh1023/p/10011290.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月23日 14时34分23秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Druid连接池实现自定义场景的多数据库的连接
2019-04-27
PL/SQL数据库管理工具的使用
2019-04-27
带你玩转属于自己的spring-boot-starter系列(一)
2019-04-27
带你玩转属于自己自己的spring-boot-starter系列(二)
2019-04-27
带你玩转属于自己的spring-boot-starter系列(三)
2019-04-27
基于SnowFlake算法如何让分库分表中不同的ID落在同一个库的算法的实现
2019-04-27
FTP文件管理项目(本地云)项目日报(二)
2019-04-27
FTP文件管理项目(本地云)项目日报(三)
2019-04-27
FTP文件管理项目(本地云)项目日报(七)
2019-04-27
什么是服务熔断?
2019-04-27
服务器压力过大?CPU打满?我来帮你快速检查Linux服务器性能
2019-04-27
C++面经总结之《Effective C++》(一)
2019-04-27
C++面经总结之《Effective C++》(二)
2019-04-27
这是什么“虎狼之词”啊!!!程序员的健康问题,看一线老中医怎么说!!!
2019-04-27
打开我的收藏夹 -- Python数据分析杂谈
2019-04-27
linux shell — 6.初识 EXT2 文件系统
2019-04-27
python - 【用户、商品】【购买、浏览】数据处理
2019-04-27