用户输入
使用input()函数可获取用户输入的文本,并以字符串的形式存入变量中。
注意:input括号内的语句只是输入提示,与input接收并存储进变量中的内容无关。
name = input("Please enter your name: ") print("Hello, " + name + "!")
当输入Carl时的输出:
Please enter your name: Carl
Hello, Carl!
有时候,输入提示可能超过一行,此时可在外部编辑要输入的提示语句,并将其存储在一个变量中,再将该变量传递给函数input()。
下例就是使用+=运算符创建多行字符串的一种方式(可理解为字符串的拼接)。
prompt = "Please tell us who you are." prompt += " What's your first name? " name = input(prompt) print(" Hello, " + name + "!")
由于Python将用户输入解读为字符串,所以当要让Python将输入视为数值时,需要调用int()函数做类型转换。
age = input("How old are you?") '''输入19''' age = int(age) age >= 18 '''True'''
while循环
1、简单的while循环
current_number = 1 while current_number <= 5: print(current_number) current_number += 1
输出为:
1 2 3 4 5
在要求很多条件都满足才能继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态,这个变量被称为标志。当标志的值为True时,程序可继续进行;当标志的值为False时,程序停止运行。
prompt = "Tell me something, and I will repeat it back to you: " prompt += " Enter 'quit' to end the program. " active = True while active: message = input(prompt) if message == 'quit': active = False else: print(message)
多次输入,得到输出为:
Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. City City Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. Both Both Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. quit
2、break语句
在循环中使用break语句,可直接退出循环。
prompt = "Tell me something, and I will repeat it back to you: " prompt += " Enter 'quit' to end the program. " while True: message = input(prompt) if message == 'quit': break else: print(message)
3、continue语句
在循环中使用continue语句,可让程序跳过在循环体中位于continue语句后面的所有语句,直接回到循环开始并开始下一轮的循环。
current_number = 0 while current_number < 10: current_number += 1 if current_number %2 == 0: continue print(current_number)
输出为:
1 3 5 7 9
使用while循环处理列表和字典
1、在列表之间移动元素
'''验证用户''' unconfirmed_users = ['alice', 'brian', 'candance'] confirmed_users = [] '''逐一验证用户''' while unconfirmed_users: current_user = unconfirmed_users.pop() print("Verifying user: " + current_user.title()) confirmed_users.append(current_user) '''显示所有已验证的用户''' print(" The following users have been confirmed: ") for confirmed_user in confirmed_users: print(confirmed_users.title())
2、删除列表中的特定值
使用循环和.remove()方法,可剔除列表中所有的特定元素。
pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat'] print(pets) while 'cat' in pets: pets.remove('cat') print(pets)
输出为:
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat'] ['dog', 'dog', 'goldfish', 'rabbit']
3、将输入存储进字典
responses = {} '''设置标志''' polling_active = True while polling_active: '''获取需要加入字典的键-值对信息''' name = input(" What's your name? ") response = input("which mountain would you like to climb someday?") '''将键-值对录入字典''' responses[name] = response '''是否要执行下一轮循环''' repeat = input("Asking another person?(yes/no)") if repeat == 'no': polling_active = False '''调查结束,显示结果''' print(" --- Poll Results ---") for name, response in responses.items(): print(name + " would like to climb " + response + ".")
输入一些信息,得到输出为:
What's your name? Eric which mountain would you like to climb someday?Denali Asking another person?(yes/no)yes What's your name? Lynn which mountain would you like to climb someday?Devil's Thumb Asking another person?(yes/no)no --- Poll Results --- Eric would like to climb Denali. Lynn would like to climb Devil's Thumb.
参考书籍:《Python编程:从入门到实践》
2020-07-12