網站首頁 編程語言 正文
使用while循環
num = 1
while num <= 10:
print(num)
num += 1
讓用戶選擇何時推出循環
print("tell me something\n")
message = ""
while message != "quit":
message = input("message:")
print(message)
print("tell me something\n")
message = ""
while message != "quit":
message = input("message:")
if message != "quit":
print(message)
退出時不會將quit也打印出來
使用標志判斷程序是否處于活動狀態,應為可能需要多個變量決定程序是否需要繼續執行,此例中極active
print("tell me something\n")
active = True
message = ""
while active:
message = input("message:")
if message == "quit":
active = False
else:
active = True
print(message)
使用break推出循環
print("tell me something\n")
active = True
message = ""
while active:
message = input("message:")
if message == "quit":
break
else:
active = True
print(message)
continue語句
當程序執行到continue時,程序判斷繼續執行
在while語句中在列表之間移動元素
un = ["A", "B", "C"]
ed = []
while un:
ing = un.pop()
print(f"ing>ed:{ing}")
ed.append(ing)
for ing in ed:
print(ing)
刪除列表中的其特定元素
pets = ["A", "B", "C"]
print(pets)
while 'B' in pets:
pets.remove('B')
print(pets)
使用用戶輸入填充字典
這是一個調查程序,收集調查者的姓名和回答
responses = {}
active = True 設置標志指出程序是否繼續執行
while active:
name = input("please input the name:") 提示輸入調查者的姓名和回答
response = input("please input the response_you:")
responses[name] = response
repeat = input("please choose whether to go Y/N:") 判斷是否還有人要參與調查
if repeat == "N":
active = False
print("\n調查結束")
for name, response in responses.items():
print(f"{name} : {response}")
原文鏈接:https://blog.csdn.net/Qy_T543/article/details/127032270
相關推薦
- 2022-05-12 linq中的串聯操作符_實用技巧
- 2022-06-24 淺析C++?atomic?和?memory?ordering_C 語言
- 2023-10-16 微信小程序radio單選按鈕選中與取消
- 2022-12-06 nginx?pod?hook鉤子優雅關閉示例詳解_nginx
- 2022-09-25 引入DjangoRESTframework
- 2023-03-16 ProxyWidget和Element更新的正確方式詳解_Android
- 2022-03-17 詳解Docker在哪里保存日志文件_docker
- 2022-03-14 flutter 列表左右滑動帶出按鈕選項
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支