##关于while循环的思考
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)
print(message)
上面的程序,用户在输入quit后,随人可以退出循环,但是:‘quit’也会被打印出来!解决这个问题有以下几种方案!
message = ""
while message != 'quit':
input(prompt)
if message != 'quit':
print(message)
active = true
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print(message)
while True:
message = input(prompt)
if message == 'quit':
break
else:
print(message)
例子.从1到10只打印其中的偶数:
# current_number = 0;
# while current_number <= 10:
# if current_number%2 == 0:
# current_number = current_number + 1
# continue
# else:
# print(str(current_number))
# current_number = current_number + 1