python编程:从入门到实践学习笔记-用户输入和while循环

函数input()

input() :暂停程序,等待用户输入文本并把文本存储在指定变量中。并接受一个参数,即要向用户显示的提示或说明。
int() :使用input()函数时,输入的文本会被当成字符串,所以可以用这个函数将其化为int型。
% :求模运算符。讲两个数相除并返回余数。
下面举个例子:

message = input("Tell me your name:")
print(message) 
message = input("Tell me your years:")
if int(message) >= 18:
    print('wow')
    print(int(message) % 6)

#运行结果:
Tell me your name:mike
mike
Tell me your years:19
wow
1

while循环

循环简介
while循环不断运行,直到指定的条件不满足位置。例如:

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

#运行结果:
1
2
3
4
5

还可以让用户选择何时退出,只要把循环的条件由current_number <= 5改为message != 'quit',然后在循环语句里面添加message = input(prompt),即:

current_number = 1
while message != 'quit':
    message = input('Enter 'quit' to end the program or go on:')
    print(current_number)
    current_number += 1 

#运行结果:
Enter 'quit' to end the program or go on: 
1
Enter 'quit' to end the program or go on: 
2
Enter 'quit' to end the program or go on:quit

这时候我们提示了一条消息:要么输入quit来结束程序,要么继续运行。当然我们还可以设置标志来决定循环的条件,比如利用布尔值True或者False

退出循环
break :不管条件测试结果如何,立即退出循环,不在运行循环中余下代码。

prompt = "\nPlease enter a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.) "
while True:
    city = input(prompt)
    if city == 'quit':
        break 

#运行结果:
Please enter a city you have visited:
(Enter 'quit' when you are finished.) China
I'd love to go to China!

Please enter a city you have visited:
(Enter 'quit' when you are finished.) San Francisco
I'd love to go to San Francisco!

Please enter a city you have visited:
(Enter 'quit' when you are finished.) quit

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

使用循环处理列表和字典

for循环中不应修改列表,因为for循环中修改列表之后,python的遍历会出现问题。而要想遍历列表并修改,可以用while循环。下面举几个例子。

在列表之间移动元素

#创建待验证用户列表和一个已验证用户列表
unconfirmed_ids = ['mike', 'lili', 'ace']
confirmed_ids = [] 
#验证每个用户并把其移动到已验证用户列表
while unconfirmed_ids:
    current_id = unconfirmed_ids.pop()
    print("Verifying id: " + current_id.title())
    confirmed_ids.append(current_id)

#显示所有已验证id
print("\nThe following ids have been confirmed:")
for confirmed_id in confirmed_ids:
    print(confirmed_id.title()) 

#运行结果:
Ace
Lili
Mike

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

nums = ['1', '3', '5', '1', '6', '1', '7']
print(nums)
while '1' in nums:
    pets.remove('1')
print(nums)

#运行结果:
['3', '5', '6', '7']

使用用户输入来填充字典

responses = {}
#设置一个标志Lj指出调查是否继续
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 + ".") 

#运行结果:
What is your name? Eric
Which mountain would you like to climb someday? Denali
Would you like to let another person respond? (yes/ no) yes

What is your name? Lynn
Which mountain would you like to climb someday? Devil's Thumb
Would you like to let another person respond? (yes/ no) no

--- Poll Results ---
Lynn would like to climb Devil's Thumb.
Eric would like to climb Denali.

    原文作者:七月
    原文地址: https://zhuanlan.zhihu.com/p/48436016
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞