python入门-WHILE循环,python入门-while,1 使用while循
python入门-WHILE循环,python入门-while,1 使用while循
1 使用while循环
current_number= 1while current_number <= 5: print(current_number) current_number += 1
2 让用户选择退出
prompt = "tell me somthing, and i will repeat it back to you"prompt = "\nEnter ‘quit‘ to end the program"message = ""while message != ‘quit‘: message=input(prompt) if message!=‘quit‘: print(message)
3 使用标志
prompt = "tell me somthing, and i will repeat it back to you"prompt = "\nEnter ‘quit‘ to end the program"active = Truewhile active: message = input(prompt) if message == ‘quit‘: active = False else: print(message)
4 使用break退出循环
prompt = "\nPlease enter the name of a city you have visited:"prompt += "\n(enter ‘quit‘ when you are finished.)"while True: city = input(prompt) if city == ‘quit‘: break else: print("I‘d love to go to" + city.title() + "!")
5 在循环中使用continue
current_number = 0while current_number < 10: current_number += 1 if current_number % 2 ==0: continue print(current_number)
6 使用while循环来处理列表和字典
unconfirmed_users = [‘alice‘,‘brian‘,‘candace‘]confirmed_users = []while unconfirmed_users: current_user = unconfirmed_users.pop() print("verifying user:" + current_user.title()) confirmed_users.append(current_user)print("\n the following users have been confirmed:")for confirmed_user in confirmed_users: print(confirmed_user.title())
7 删除特定的列表元素
pets = [‘dog‘,‘cat‘,‘dog‘,‘goldfish‘,‘cat‘,‘rabbit‘,‘cat‘]print(pets)while ‘cat‘ in pets: pets.remove(‘cat‘)print(pets)
8使用用户输入来填充字典
responses = {}polling_active = Truewhile polling_active: name = input("\n what is your name?") response = input("which mountain would you like to climb someday") responses[name] = response repeat = input("would you like to let another person respond?(yes or no") if repeat == ‘no‘: polling_active = Falseprint("\n --poll results--")for name,response in responses.items(): print(name + "would like to climb" + response + ".")
python入门-WHILE循环
相关内容
- python 列表中的Update()函数,pythonupdate,简介Python 字
- python之使用request模块发送post和get请求,pythonrequest,im
- 史上最全python面试题详解 (二)(附带详细答案(关注
- Python+Appium学习之启动手机浏览器,pythonappium,一、启动
- 微信运动数据抓取(Python),抓取python,“微信运动”能够
- Python编程:从入门到实践——【作业】——第六章作业
- Python 中三大框架各自的应用场景??,python三大,django:主
- python3 for循环-range遍历,python3-range,for循环、rang
- python学习第一模块练习,python模块练习,#!/usr/bin
- python学习记录(十)--------循环语句,python--------,循环
评论关闭