Python中的循环和标准输入入门教程

177 阅读5分钟

Python中的循环和标准输入入门

作为一名程序员,你要写的许多程序都会解决一个最终用户的问题。你总是想从用户那里获得一些数据。本文将解释如何处理用户输入以及如何使用while循环。

前提条件

  1. 在你的计算机上安装Python
  2. 基本的Python知识。

目标

在这篇文章中,我们将了解到。

  • input() 函数如何工作
  • 使用int() 来接受数字输入
  • 介绍while 循环
  • 使用while 循环和else 语句
  • 使用break退出while 循环
  • 使用while 循环从一个列表中删除所有特定值的实例
  • 使用while 循环将用户输入的信息填入一个字典中

input()函数如何工作

input() 函数停止程序的执行,等待用户输入一些数据。当 Python 收到用户的输入时,它将输入的数据存储在你选择的变量中。

例如,让我们创建一个程序,接受一个用户的名字并打印回这个名字。

name = input("Please enter your name: ")
print(name)
#output
#Please enter your name: John
#John

input() 函数接收一个参数,也就是你希望用户看到的指令。在这个例子中,Python执行第一行,要求用户输入他/她的名字。程序执行将停止并等待用户输入他/她的名字,在键盘上按下ENTER 键后继续。然后,用户的名字被加载到变量name ,然后print(name) ,显示用户的名字回来。

使用int()来接受数字输入

用户使用input() 函数输入的任何文本,都被解释为一个字符串。如果你只需要打印出输入的内容,那么使用input() 函数就可以了。直接将输入值作为一个数值使用会产生一个异常。请看下面的例子。

amount = input("Please enter your amount? ")
rate=0.056
interest= amount*rate
print("Your interest is : ")
print(interest)

上面的例子抛出了一个错误,因为Python将输入的amount 解释为一个字符串。Python不能将字符串和浮点数相乘。

为了解决上述问题,我们使用int() 函数,该函数通知 Python 将输入作为一个数值使用。int() 函数将字符串转换为整数,如下所示。

amount = input("Please enter your amount? ")
amount=int(amount)
rate=0.056
interest= amount*rate
print("Your interest is : ")
print( interest)

输出。

Please enter your amount? 9000
Your interest is : 
504.0

当我们在上面的例子中输入9000时,Python将其解释为一个字符串。然后使用int() 函数将该金额转换为整数。现在Python计算出了利息的值。

介绍一下while 循环

在Python中,一个while 循环执行一个给定的代码块,前提是某些条件保持为真。

语法。

while expression:
 statement(s)

下面这个while 循环从10数到15。

count = 10
while (count <=15):
 print ('The count is:', count)
 count = count + 1 #add 1 to count
#output
#The count is: 10
#The count is: 11
#The count is: 12
#The count is: 13
#The count is: 14
#The count is: 15

如果count 的值等于或小于15,则while 循环继续执行。

在while循环中使用else语句

当你在while循环中使用else 语句时,它只会在条件变为假时执行。

下面的例子同时涉及else 语句和while 语句。

count = 10
while (count <=15):
 print ('The count is:', count)
 count = count + 1
else:
 print(count, " is not less than 5")

上述程序打印出一个小于或等于15的数字,否则else 块就会执行。

结果。

The count is: 11
The count is: 12
The count is: 13
The count is: 14
The count is: 15
16 is not less than 15

使用break来退出一个循环

使用break 语句来退出一个while 循环,而不执行循环中的任何剩余代码。使用break 语句,你可以控制程序中哪些行将执行或不执行。

语法。

while expression:
 #code for while loop
 if_expression:
 break
 #code for while loop
# code outside of while loop

例子。

i = 1
while i < 11:
 if i == 6:
 break
 print(i)
 i = i + 1
print('Bye')
#output
#1
#2
#3
#4
#5
#Bye

使用while循环从一个列表中删除所有特定值的实例

remove() 方法只从一个列表中删除一个值。我们使用while 循环从一个列表中删除一个值的所有场合。

假设我们有一个雇员列表,名字John 出现了不止一次。我们可以使用while 循环来删除所有名称John 的实例。while 循环一直执行到John 不再出现在列表中,如下图所示。

employees = ['Mary', 'John', 'Paul', 'John', 'Yusuf', 'John'] #list containing many instances of 'John'
print(employees) # ['Mary', 'John', 'Paul', 'John', 'Yusuf', 'John']
while 'John' in employees:
 employees.remove('John')
print(employees) #['Mary', 'Paul', 'Yusuf']

使用 while 循环将用户输入的信息填入一个字典中

我们使用while 循环来提示用户输入我们需要的信息。让我们创建一个程序,接受用户名和每个用户喜欢爬的山的名字。由于我们想把每个响应与一个特定的用户联系起来,我们将把数据存储在一个字典中。

responses = {} # define an empty dictionary
# Set a flag to show that polling is active.
polling_active = True
while polling_active:
# Prompt for the person's name and response.
 name = input("\nEnter your name? ")
 response = input("Enter the name of the mountain you would like to climb? ")
 responses[name] = response # Store the response in the dictionary:
 # Find out if anyone else is going to take the poll.
 repeat = input("Please refer another person? (yes/ no) ")
 if repeat == 'no':
 polling_active = False
print("\n....Poll Results...")
for name, response in responses.items():
 print(name + " wishes to climb " + response + ".") # print results of the poll

当你执行这个程序并输入一些响应时,输出结果应该是这样的。

Enter your name? Peter
Enter the name of the mountain you would like to climb? Everest
Please refer another person? (yes/ no) yes
Enter your name? John
Enter the name of the mountain you would like to climb? Turin
Please refer another person? (yes/ no) no
....Poll Results...
Peter wishes to climb Everest.
John wishes to climb Turin.

结论

现在你已经学会了如何使用input() 函数和while 循环,试着在你的 Python 程序中实现它们。