第7章 用户输入和while循环

Posted dingdangsunny

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第7章 用户输入和while循环相关的知识,希望对你有一定的参考价值。

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

message = input("Tell me something, and I will repeat it back to you: ")
print(message)

输出
Tell me something, and I will repeat it back to you: Hello everyone!
Hello everyone!

height = input("How tall are you, in inches? ")
height = int(height)#int()用于将获取的输入转换为整型数格式
if height >= 36:
print("
You‘re tall enough to ride! ")
else:
print("
You‘ll be able to ride when you‘re a little older. ")

while循环

number = 1
while number <= 5:
print(number)
number += 1

遇到break,立即跳出循环
遇到continue,结束本次循环,进入下一次循环

使用用户输入来填充字典
responses = {}
设置一个标志位,指出调查是否继续

polling_active = True
while polling_active:
name = input("
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/no)")
if repeat =="no":
polling_active = False
print("
--- Poll Result ---")
for name, response in responses.items():
print(name.title() + "would like to climb" + response + ".")

以上是关于第7章 用户输入和while循环的主要内容,如果未能解决你的问题,请参考以下文章

Python编程入门到实践 - 笔记( 7 章)

Class - 7 用户输入和while循环

Python初级-7用户输入和while循环

第六章编程练习7

第2章 基本语法(下):程序流程控制

使用用户输入中断while循环(通过arduino和python 2.7控制neopixels)