# 函数input() 让程序暂停运行,等待用户输入一些文本。 # 获取用户输入后,Python将其存储在一个变量中,以方便你使用 message = input("Tell me something, and I will repeat it back to you: ") print(message) name = input("Please enter your name: ") print("Hello, " + name + "!") # 输入数字,输出是字符串 age = input("how old are you?") print(age) age = int(age) print(age > 18) # 使用while循环 current_number = 1 while current_number <= 5: print(current_number) current_number += 1 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) if message != 'quit': print(message) # 以while True 打头的循环(见❶)将不断运行,直到遇到break 语句 prompt = "\nTell me something, and I will repeat it back to you: " prompt += "\nEnter 'quit' to end the program." while True: message = input(prompt) if message != 'quit': print(message) else: break # continue跳出本次循环 current_number = 0 while current_number < 10: current_number += 1 if current_number % 2 == 0: continue else: print(current_number) prompt = "\nAge: " while True: message = input(prompt) if message == 'quit': break elif int(message) < 3: print('free') elif int(message) <= 12: print(10) else: print(15) # 在列表之间移动元素 unconfirmed_users = ['alice', 'brian', 'candace'] confirmed_users = [] while unconfirmed_users: current_user = unconfirmed_users.pop() confirmed_users.append(current_user) print("the following users have been confirmed: ") for confirmed_user in confirmed_users: print(confirmed_user.title()) # 删除包含特定值的所有列表元素 pets = ['dogs', 'cat', 'pig', 'cat', 'rabbit', 'cat'] print(pets) while 'cat' in pets: pets.remove('cat') print(pets) # 使用用户输入来填充字典 responses = {} polling_active = True while polling_active: name = input("what is your name? ") response = input("which mountain would you like to climb someday? ") responses[name] = response repeat = input("any one else? ") if repeat == 'no': polling_active = False for name, response in responses.items(): print(name + ' would like to climb ' + response + '.')