Python编程:从入门到实践(读书笔记:第7章 用户输入和while循环)

coding:utf-8!

chapter 7 用户输入和while循环

函数input()提示用户输入内容

运用while循环控制程序运行时间

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

print("\n")

sublime text代码编辑器

name = input("Please enter your name: ")
print("Hello, " + name + “!”)

print("\n")

prompt提示

prompt = “If you tell us who you are, we can personalize the messages you see.”
prompt += "\nWhat is your first name? " # +=将后面的字串符加在prompt末尾(这句可省略)

注意:上面不能写成:print(prompt + "\nWhat is your first name? ")

name = input(prompt)
print("Hello, " + name + “!”)

需要记住prompt的上下文用法

print()

使用int()获取数值输入

age = input("How old are you? ")
age = int(age) # int()将数字的字串符转换为数值表示
if age >= 18:
print(“True, it is larger than 18.”)
else:
print(“False, it is smaller than 18.”)

print()

判断一个人是否满足过山车身高要求

height = input(“How tall are you, in inches?”)
height = int(height)
if height >= 36:
print("\nYou’re tall enough to ride!")
else:
print("\nYou’ll be able to ride when you’re a litter older.")

print()

求模运算符%将2个数相除并返回余数

print(4%3)
print(5%3)
print(6%3)
print(7%3)

print()

利用求模运算符%判断奇偶数

number = input("Enter a number, and I’ll tell you if it’s even or odd: ")
number = int(number)
if number % 2 == 0:
print(“The number " + str(number) + " is even.”)
else:
print(“The number " + str(number) + " is odd.”)

如果用的是Python2.7,需要用函数raw_inpu()来代替Python3里面的input(),否则会错误

for循环针对集合中每个元素的一个代码块

while循环会不断运行直到条件不满足为止

print()

使用while循环数数

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

让客户选择何时退出

prompt = "\nTell me something, and I will repeat it back to you1original: "
prompt += "\nEnter ‘quit’ to end the program. "
message = “” # 创建变量message,用于存储用户输入值
while message != ‘quit’: # ‘!=’ 表示‘不等于’ # 将message与quit比较
message = input(prompt)
print(message)

print()

使用if语句修复上面程序(不打印quit)

prompt = "\nTell me something, and I will repeat it back to you2if: "
prompt += "\nEnter ‘quit’ to end the program. "
message = “”
while message != ‘quit’:
message = input(prompt)
if message != ‘quit’:
print(message)

使用标志(很多条件都满足才继续运行,可定义一个变量来判断活动状态)

prompt = "\nTell me something, and I will repeat it back to you3true: "
prompt += "\nEnter ‘quit’ to end the program. "
active = True
while active:
message = input(prompt)
if message == ‘quit’:
active = False
else:
print(message)

使用break退出循环(立即退出while循环不再运行余下代码,也不管结果如何时使用)

prompt = “\nPlease enter the name of a city you have visited: 4”
prompt += "\n(Enter ‘quit’ when you are finished.) "
while True: # 以while true打头的循环将不断运行,直到遇到break语句
city = input(prompt)
if city == ‘quit’:
break
else:
print("I’d love to go to " + city.title() + “!”)

print()

在循环中使用continue

current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue # 如果可以被2整除,就直接返回循环开头
print(current_number)

print()

避免无限循环

x = 1
while x <= 5:
print(x)
x += 1 # 如果没有这行代码,循环将没完没了地打印1。可按Ctrl+C停

使用while循环处理列表和字典

在列表之间移动元素

print()

首先,创建一个待验证用户列表 和 一个用于存储已验证用户的空列表

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("\nThe following users have been confirmed: ")
for confirmed_user in confirmed_users:
print(confirmed_user.title())

删除包含特定值的所有列表元素

print()
pets = [‘dog’, ‘cat’, ‘dog’, ‘goldfish’, ‘cat’, ‘rabbit’, ‘cat’]
print(pets)
while ‘cat’ in pets:
pets.remove(‘cat’)
print(pets)

使用用户输入来填充字典

print()
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 to 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 + “.”)

你可能感兴趣的:(Python)