python 输入和while循环



1 函数input()的工作原理
1.1 函数input() 让程序暂停运行,等待用户输入一些文本。获取用户输入后,python将其存储在一个变量中,以方便你使用。使用input函数,python会将用户输入解读为字符串。
example 1
python
 
  
message = input ( "Tell me something,and I will repeat it back to you:" )
 
  
print ( message )
1.2 在提示末尾包含一个空格,可将提示与用户输入分开,让用户清楚地知道其输入始于何处。
example 2
python
 
  
prompt = "If you tell us who you are,we can personalize the messages you see."
 
  
prompt += "\nWhat is your first name?"
 
  
name = input ( prompt )
 
  
print("\nHello,"+name+"!")
1.3 使用int()来获取数值输入
可以使用int函数对用户输入的数值,从字符串的形式转变为数值。
example 3
python
 
  
>> > age = input ( "How old are you?" )
 
  
How old are you?19
 
  
>>>age=int(age)
 
  
>>>age>=18
 
  
True
1.4 使用%函数将两个数相除并返回余数
example 4
python
 
  
>> > 4 % 3
 
  
1

2 While循环
2.1 与for循环的区别
for 循环用于针对集合中的每个元素的一个代码块,而while循环不断地进行,直到指定的条件不满足。
while 循环的规则
python
 
  
while 循环执行的条件:
 
  
循环的操作
example 5
python
 
  
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 )
 
  
if message != 'quit' :
 
  
print ( message )
2.2 使用标志
可定义一个变量,用于判断整个程序是否处于活动状态。
example 6
python
 
  
prompt = "\nTell 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 )
2.3 使用break退出循环
break语句用于控制程序流程,可使用它来控制哪些代码将执行,哪些代码不执行。
2.4 使用continue
continue作用:使用continue,根据条件测试结果决定是否继续执行循环,返回到循环开头。
example 7
python
 
  
number = 0
 
  
while number < 10 :
 
  
number += 1
 
  
if number % 2 == 0 :
 
  
continue
 
  
print ( number )
2.5 避免无限循环
Tips:如果程序陷入无限循环,可按Ctrl+C,停止循环。

3 使用while循环来处理字典和列表
3.1 在列表之间移动元素
example 8
python
 
  
#首先,创建一个待验证用户列表
 
  
#和一个用于存储以验证用户的空列表
 
  
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_users . title ( ) )
3.2 删除包含特定值的所有列表元素
python
 
  
pets = [ 'dog' , 'cat' , 'dog' , 'goldfish' , 'cat' , 'rabbit' , 'cat' ]
 
  
print ( pets )
 
  
while 'cat' in pets :
 
  
pets . remove ( 'cat' )
 
  
print ( pets )
3.3 使用用户输入来填充字典
example 9
python
 
  
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 + "." )
运行结果如下:
python
 
  
What is your name?Jerry
 
  
Which mountain would you like to climb someday?lu
 
  
Would you like to let another person respond? ( yes / no ) yes
 
  

 
  
What is your name?Tom
 
  
Which mountain would you like to climb someday?tai
 
  
Would you like to let another person respond? ( yes / no ) no
 
  

 
  
- - - - Poll Results - - - -
 
  
Jerry Would like to climb lu .
 
  
Tom Would like to climb tai .

1 函数input()的工作原理
1.1 函数input() 让程序暂停运行,等待用户输入一些文本。获取用户输入后,python将其存储在一个变量中,以方便你使用。使用input函数,python会将用户输入解读为字符串。
example 1
python
 
   
message = input ( "Tell me something,and I will repeat it back to you:" )
 
   
print ( message )
1.2 在提示末尾包含一个空格,可将提示与用户输入分开,让用户清楚地知道其输入始于何处。
example 2
python
 
   
prompt = "If you tell us who you are,we can personalize the messages you see."
 
   
prompt += "\nWhat is your first name?"
 
   
name = input ( prompt )
 
   
print("\nHello,"+name+"!")
1.3 使用int()来获取数值输入
可以使用int函数对用户输入的数值,从字符串的形式转变为数值。
example 3
python
 
   
>> > age = input ( "How old are you?" )
 
   
How old are you?19
 
   
>>>age=int(age)
 
   
>>>age>=18
 
   
True
1.4 使用%函数将两个数相除并返回余数
example 4
python
 
   
>> > 4 % 3
 
   
1

2 While循环
2.1 与for循环的区别
for 循环用于针对集合中的每个元素的一个代码块,而while循环不断地进行,直到指定的条件不满足。
while 循环的规则
python
 
   
while 循环执行的条件:
 
   
循环的操作
example 5
python
 
   
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 )
 
   
if message != 'quit' :
 
   
print ( message )
2.2 使用标志
可定义一个变量,用于判断整个程序是否处于活动状态。
example 6
python
 
   
prompt = "\nTell 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 )
2.3 使用break退出循环
break语句用于控制程序流程,可使用它来控制哪些代码将执行,哪些代码不执行。
2.4 使用continue
continue作用:使用continue,根据条件测试结果决定是否继续执行循环,返回到循环开头。
example 7
python
 
   
number = 0
 
   
while number < 10 :
 
   
number += 1
 
   
if number % 2 == 0 :
 
   
continue
 
   
print ( number )
2.5 避免无限循环
Tips:如果程序陷入无限循环,可按Ctrl+C,停止循环。

3 使用while循环来处理字典和列表
3.1 在列表之间移动元素
example 8
python
 
   
#首先,创建一个待验证用户列表
 
   
#和一个用于存储以验证用户的空列表
 
   
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_users . title ( ) )
3.2 删除包含特定值的所有列表元素
python
 
   
pets = [ 'dog' , 'cat' , 'dog' , 'goldfish' , 'cat' , 'rabbit' , 'cat' ]
 
   
print ( pets )
 
   
while 'cat' in pets :
 
   
pets . remove ( 'cat' )
 
   
print ( pets )
3.3 使用用户输入来填充字典
example 9
python
 
   
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 + "." )
运行结果如下:
python
 
   
What is your name?Jerry
 
   
Which mountain would you like to climb someday?lu
 
   
Would you like to let another person respond? ( yes / no ) yes
 
   

 
   
What is your name?Tom
 
   
Which mountain would you like to climb someday?tai
 
   
Would you like to let another person respond? ( yes / no ) no
 
   

 
   
- - - - Poll Results - - - -
 
   
Jerry Would like to climb lu .
 
   
Tom Would like to climb tai .

猜你喜欢

转载自blog.csdn.net/lzj_1314/article/details/79765139