message = input("Tell me something, and I will repeat it back to you: ")
print(message)
print("\n")
name = input("Please enter your name: ")
print("Hello, " + name + “!”)
print("\n")
prompt = “If you tell us who you are, we can personalize the messages you see.”
prompt += "\nWhat is your first name? " # +=将后面的字串符加在prompt末尾(这句可省略)
name = input(prompt)
print("Hello, " + name + “!”)
print()
age = input("How old are you? ")
age = int(age) # int()将数字的字串符转换为数值表示
if age >= 18:
print(“True, it is larger than 18.”)
else:
print(“False, it is smaller than 18.”)
print()
height = input(“How tall are you, in inches?”)
height = int(height)
if height >= 36:
print("\nYou’re tall enough to ride!")
else:
print("\nYou’ll be able to ride when you’re a litter older.")
print()
print(4%3)
print(5%3)
print(6%3)
print(7%3)
print()
number = input("Enter a number, and I’ll tell you if it’s even or odd: ")
number = int(number)
if number % 2 == 0:
print(“The number " + str(number) + " is even.”)
else:
print(“The number " + str(number) + " is odd.”)
print()
current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1
prompt = "\nTell me something, and I will repeat it back to you1original: "
prompt += "\nEnter ‘quit’ to end the program. "
message = “” # 创建变量message,用于存储用户输入值
while message != ‘quit’: # ‘!=’ 表示‘不等于’ # 将message与quit比较
message = input(prompt)
print(message)
print()
prompt = "\nTell me something, and I will repeat it back to you2if: "
prompt += "\nEnter ‘quit’ to end the program. "
message = “”
while message != ‘quit’:
message = input(prompt)
if message != ‘quit’:
print(message)
prompt = "\nTell me something, and I will repeat it back to you3true: "
prompt += "\nEnter ‘quit’ to end the program. "
active = True
while active:
message = input(prompt)
if message == ‘quit’:
active = False
else:
print(message)
prompt = “\nPlease enter the name of a city you have visited: 4”
prompt += "\n(Enter ‘quit’ when you are finished.) "
while True: # 以while true打头的循环将不断运行,直到遇到break语句
city = input(prompt)
if city == ‘quit’:
break
else:
print("I’d love to go to " + city.title() + “!”)
print()
current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue # 如果可以被2整除,就直接返回循环开头
print(current_number)
print()
x = 1
while x <= 5:
print(x)
x += 1 # 如果没有这行代码,循环将没完没了地打印1。可按Ctrl+C停
print()
unconfirmed_users = [‘alice’, ‘brian’, ‘candace’]
confirmed_users = []
while unconfirmed_users:
current_user = unconfirmed_users.pop()
print("Verifying user: " + current_user.title())
confirmed_users.append(current_user)
print("\nThe following users have been confirmed: ")
for confirmed_user in confirmed_users:
print(confirmed_user.title())
print()
pets = [‘dog’, ‘cat’, ‘dog’, ‘goldfish’, ‘cat’, ‘rabbit’, ‘cat’]
print(pets)
while ‘cat’ in pets:
pets.remove(‘cat’)
print(pets)
print()
responses = {}
polling_active = True
while polling_active:
# 提示输入被调查者的名字和回答
name = input("\nWhat is your name? ")
response = input("Which mountain would you like to climb someday? ")
# 将答案存储在字典中
responses[name] = response
# 看看是否还有人要参与调查
repeat = input(
"Would you like to let another person to respond?(yes/no) ")
if repeat == ‘no’:
polling_active = False
print("\n— Poll Results —")
for name, response in responses.items():
print(name + " would like to climb " + response + “.”)