input 工作原理

函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中。

message = input("need to  input string ")
print(message) input() 接受一个参数作为提示,程序等待用户输入后,在用户回车确认后继续运行,输入存储在变量中 或者: promt = "hello plse input you name"
promt += "\nyou fist name " input(promt)

使用 int()来获取数值输入

使用函数input()时,Python将用户输入解读为字符串,将输入作为数字使用,使用函数init()

for循环

a = ['ks','dkd','jdjd',10,]
for xx in a:
print(xx)

求模运算

处理数值信息时,求模运算符(%)是一个很有用的工具,它将两个数相除并返回余数。

求模运算符不会指出一个数是另一个数的多少倍,而只指出余数是多少

如果一个数可被另一个数整除,余数就为0,因此求模运算符将返回0。你可利用这一点来判断一个数是奇数还是偶数

number % 2 == 0:

while循环

current_number = 1
while current_number <= 5:
print( str(current_number) + "回合")
current_number += 1
print(current_number)

让用户选择何时退出

prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
message = ""
while message != 'quit':
message = input(prompt)
print(message)

break 退出循环

立即退出while循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用break语句

prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.) "
print(prompt) active = True
while active :
xx = input(prompt)
if xx == "quit" :
active = False
else :
print(xx)

break循环

prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.) "
print(prompt) while True :
xx = input(prompt)
if xx == "quit" :
break
else :
print(xx)

循环中使用continue

返回到循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue语句,它不像break语句那样不再执行余下的代码并退出整个循环


current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue
print(current_number) 首先将current_number设置成了0,由于它小于10,Python进入while循环。进入循环后,我们以步长1的方式往上数 ,因此current_number为1。接下来,if语句检查current_number与2的求模运算结果。如果结果为0(意味着current_number可被2整除),就执行continue语句,
让Python忽略余下的代码,并返回到循环的开头。如果当前的数字不能被2整除,就执行循环中余下的代码,Python将这个数字打印出来

避免无限循环

while 循环处理列表和字典

for循环是一种遍历列表的有效方式,但在for循环中不应修改列表,否则将导Python难以跟踪其中的元素。要在遍历列表的同时对其进行修改,可使用while循环

unconfirmed_users = ['alice', 'brian', 'candace']
confirmed_users = []
while unconfirmed_users:
current_user = unconfirmed_users.pop()
print(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', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
print(pets)
while 'cat' in pets:
pets.remove("cat") print(pets)

总结

break语句可以在循环过程中直接退出循环,而continue语句可以提前结束本轮循环,并直接开始下一轮循环。这两个语句通常都必须配合if语句使用。

使用

用户输入来填充字典

responses = {}
# 设置一个标志,指出调查是否继续
polling_active = True
while polling_active:
# 提示输入被调查者的名字和回答
name = input("\nWhat 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/ no) ")
if repeat == 'no':
polling_active = False # 调查结束,显示结果
print("\n--- Poll Results ---")
for name, response in responses.items():
print(name + " would like to climb " + response + ".")

用户输入和while 循环的更多相关文章

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

    Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...

  2. python入门学习:6.用户输入和while循环

    python入门学习:6.用户输入和while循环 关键点:输入.while循环 6.1 函数input()工作原理6.2 while循环简介6.3 使用while循环处理字典和列表 6.1 函数in ...

  3. 用户输入与while循环

    函数input()的工作原理: 函数input()让程序短暂运行,等待用户输入一些文本,获取用户输入后将其存储在一个变量中 测试input()功能-- #!/usr/bin/env python#fi ...

  4. python从入门到实践-7章用户输入和while循环

    #!/user/bin/env python# -*- coding:utf-8 -*- # input() 可以让程序暂停工作# int(input('please input something: ...

  5. Python:从入门到实践--第七章--用户输入和while循环-练习

    #1.编写一个程序,询问用户要租赁什么样的汽车,并打印. car = input("What's kind of cars dou you want to rent?,sir:") ...

  6. 《Python编程从入门到实践》_第七章_用户输入和whlie循环

    函数input()的工作原理 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,python将其存储在一个变量中,以方便你使用. #输入用户名 username = input( ...

  7. python的用户输入和while循环

    1.函数input()工作原理 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便你使用. (1)获取数值可以用 int()函数 (2)求 ...

  8. 读书笔记「Python编程:从入门到实践」_7.用户输入和while循环

    7.1 函数input()的工作原理 函数input() 让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便你使用. message = input(&qu ...

  9. 第七章 用户输入和while 循环

    7.1 创建多行字符串的方式: 01 prompt="if you tell me who you are, we can personalize the message you see.& ...

随机推荐

  1. XSS-存储型

    @实操视频https://www.bilibili.com/video/av26679456?from=search&seid=13377211289924067562 存储型的注入对象不是搜 ...

  2. magento下载地址

    https://download.magentochina.org/magento/2/ https://www.magentochina.org/blog/download-install-mage ...

  3. kafka语句示例

    1.从http://kafka.apache.org/下载kafka安装包:2.tar zxvf kafka_2.8.0.tar.gz,修改配置文件conf/server.properties:bro ...

  4. Excel透视表基础之字段布局与重命名、更新、数字格式设置、空值与错误值、

    字段布局与重命名 经典布局切换 字段布局 默认布局:文本类型在行区域.数字类型在值区域. 最好用鼠标拖拽. 字段重命名 可以在字段设置中更改. 透视表更新 延迟更新 手动刷新 自动刷新 刷新注意事项 ...

  5. spring + redis 实例(一)

    这一篇主要是redis操作工具类以及基本配置文本储存 首先我们需要定义一个redisUtil去操作底层redis数据库: package com.lcc.cache.redis; import jav ...

  6. python3—廖雪峰之练习(一)

    变量练习 小明的成绩从去年的72分提升到今年的85分,请计算小明成绩提升的百分点.并用 字符串格式化显示出'xx.x%',只保留小数点后一位: s1 = 72 s2 = 85 r = (85-72)/ ...

  7. List<HashMap<String,String>> list, 根据hashmap中的某个键的值排序

    来源https://blog.51cto.com/zhaodan/1725249 //可以使用Collections.sort(List list, Comparator c)来实现 这里举例hash ...

  8. JavaEE--JSP详解

    一.JSP JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它是由Sun Microsystems公司倡导.许多公司参与一起建立的一种 ...

  9. 使用T4模板为EF框架添加数据库实体注释(转)

    1. 下载文件GetSummery.ttinclude2. 把我们下载下来的文件解压,将解压出来的文件放入解决方案中3. 修改下app.config,添加一个连接字符串: <add name=& ...

  10. HTTPS到底是什么

    Http存在的问题   上过网的朋友都知道,网络是非常不安全的.尤其是公共场所很多免费的wifi,或许只是攻击者的一个诱饵.还有大家平时喜欢用的万能钥匙,等等.那我们平时上网可能会存在哪些风险呢?   ...