# input 函数 message = input("Tell me something, and I will repeat it back to you: ") print(message) # output: # Tell me something, and I will repeat it back to you: Hello! # Hello!
# select when to exit prompt = "Tell me something, and I will repeat it back to you:" prompt += "\nEnter 'quit' to end the program. input:" message = "" while message != 'quit': message = input(prompt) if message != 'quit': print(message) # output Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. input:Hello Hello Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. input:Hello again Hello again Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. input:quit
2.3使用break退出循环
1 2 3 4 5 6 7 8 9 10 11 12 13 14
prompt = "Tell me something, and I will repeat it back to you:" prompt += "\nEnter 'quit' to end the program. input:" whileTrue: city = input(prompt) if city == 'quit': break else: print(city.title()) #output Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. input:Hello Hello Tell me something, and I will repeat it back to you: Enter 'quit' to end the program. input:quit