【Python基础知识】(六)用户输入与while循环

2021-04-19 21:28

阅读:627

标签:创建   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‘‘‘

 

while循环

1、简单的while循环

current_number = 1
while current_number :
    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

 

2、break语句

  在循环中使用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 )

 

3、continue语句

  在循环中使用continue语句,可让程序跳过在循环体中位于continue语句后面所有语句,直接回到循环开始开始下一轮的循环

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

  (运行时删除中文注释,其他程序同)

 

2、删除列表中的特定值

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( "\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 + "." )

输入一些信息,得到输出为:

Whats your name? Eric
which mountain would you like to climb someday?Denali
Asking another person?(yes/no)yes

Whats your name? Lynn
which mountain would you like to climb someday?Devils Thumb
Asking another person?(yes/no)no

--- Poll Results ---
Eric would like to climb Denali.
Lynn would like to climb Devils Thumb.

 

参考书籍:《Python编程:从入门到实践》

2020-07-12

 

【Python基础知识】(六)用户输入与while循环

标签:创建   items   tle   文本   col   停止   参考   response   运算   

原文地址:https://www.cnblogs.com/carl39/p/13287773.html


评论


亲,登录后才可以留言!