如果我们需要用户进行输入,且为了更好的显示输入信息显示为两行。
巧用while循环!
prommpt="\nPlease input something , that i can repeat it to you"
prommpt+="Enter 'quit' to end the program !"
message=''
while message != "quit":
message = input(prommpt)
print(message)
在该代码中,首先我们令message为空字符串,使其进入到while循环中,接下来将message赋值为用户的输入。对用户的输入进行打印,回归到while循环,如果再次输入为“quit”,则退出此循环。
退出循环(break/执行标志)
# 使用标志
# 在要求满足较多条件时才能运行的程序时,我们可以使用一个标志来代表是否执行程序。再将该标志去激活或者抑制程序的执行
# 我们可以使用标志或者break退出循环
prompt = "Plese input somenthing that i can repeat it to you !"
prompt+= "\nEnter 'quit' to end this program !"
active = True
while active:
message = input(prompt)
if message == 'quit':
break ;
else:
print(message)
继续执行:(continue)
# 满足某个条件时需要继续执行程序
# 巧用continue,continue是把程序回到最初的循环执行。
# 对于1-10,我们打印偶数
i = 1
while i <= 10:
if i % 2 == 0:
print(i)
i += 1
else:
i += 1
continue