标签:创建 items tle 文本 col 停止 参考 response 运算
使用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 += "\nWhat‘s your first name? " name = input( prompt ) print( "\nHello, " + name + "!" )
由于Python将用户输入解读为字符串,所以当要让Python将输入视为数值时,需要调用int()函数。
age = input( "How old are you?" ) ‘‘‘输入19‘‘‘ age = int( age ) age >= 18 ‘‘‘True‘‘‘
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 += "\nEnter ‘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
在循环中使用break语句,可直接退出循环。
prompt = "Tell me something, and I will repeat it back to you: " prompt += "\nEnter ‘quit‘ to end the program. " while True: message = input( prompt ) if message == ‘quit‘: break else: print( message )
在循环中使用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
‘‘‘验证用户‘‘‘ 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( "\nThe following users have been confirmed: " ) for confirmed_user in confirmed_users: print( confirmed_users.title() )
(运行时删除中文注释,其他程序同)
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‘]
responses = { } ‘‘‘设置标志‘‘‘ polling_active = True while polling_active: ‘‘‘获取需要加入字典的键-值对信息‘‘‘ name = input( "\nWhat‘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( "\n--- 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
标签:创建 items tle 文本 col 停止 参考 response 运算
原文地址:https://www.cnblogs.com/carl39/p/13287773.html