You are tall enough to ride!
#用于判断奇数偶数
number = input("enter a number, and I‘ll tell you if it‘s even or odd:")
number = int(number)
if number % 2 == 0:
print("\nThe number " + str(number) + "is even.")
else:
print("\nThe number " + str(number) + "is odd.")
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)
Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.hihi
hihi
Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.露露
露露
Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.xixi
xixi
Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.quit
#用于判断奇数偶数
number = input("enter a number, and I‘ll tell you if it‘s even or odd:")
number = int(number)
if number % 2 == 0:
print("\nThe number " + str(number) + "is even.")
else:
print("\nThe number " + str(number) + "is odd.")
enter a number, and I‘ll tell you if it‘s even or odd:51
The number 51is odd.
#while循环
current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1
1
2
3
4
5
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)
Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.hi
hi
Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.hello
hello
Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.quit
quit
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)
Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.hi
Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.hello
Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.quit
#使用标志
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter ‘quit‘ to end the program."
active = True
while active:
message = input(prompt)
if message == ‘quit‘:
active = False
else:
print(message)
Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.hi
hi
Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.hello
hello
Tell me something, and I will repeat it back to you:
Enter ‘quit‘ to end the program.quit
#使用break退出循环
prompt = "\nPlease enter the name of a city you have visited."
prompt += "\n(enter ‘quit‘ when you are finished.)"
while True:
city = input(prompt)
if city == ‘quit‘:
break
else:
print("mmmmmm" + city.title() + "!")
Please enter the name of a city you have visited.
(enter ‘quit‘ when you are finished.)nanjing
mmmmmmNanjing!
Please enter the name of a city you have visited.
(enter ‘quit‘ when you are finished.)guangdong
mmmmmmGuangdong!
Please enter the name of a city you have visited.
(enter ‘quit‘ when you are finished.)quit
Please enter the name of a city you have visited.
(enter ‘quit‘ when you are finished.)nanjing
mmmmmmNanjing!
Please enter the name of a city you have visited.
(enter ‘quit‘ when you are finished.)guangdong
mmmmmmGuangdong!
Please enter the name of a city you have visited.
(enter ‘quit‘ when you are finished.)quit
#continue
number = 0
while number < 10:
number += 1
if number % 2 == 0:
continue
print(number)
1
3
5
7
9
#避免无限循环
#处理列表和字典
unconfirmed_users = [‘baba‘,‘mama‘,‘jing‘]
confirmed_users = []
while unconfirmed_users:
current_user = unconfirmed_users.pop()
print("vertify:" + current_user.title())
confirmed_users.append(current_user)
vertify:Jing
vertify:Mama
vertify:Baba
print("new:")
for confirmed_user in confirmed_users:
print(confirmed_user.title())
new:
Jing
Mama
Baba
#删除特定值的所有列表元素
pet = [‘dog‘,‘cat‘,‘dog‘,‘goldfish‘,‘cat‘,‘rabbit‘,‘cat‘]
print(pet)
[‘dog‘, ‘cat‘, ‘dog‘, ‘goldfish‘, ‘cat‘, ‘rabbit‘, ‘cat‘]
pets = [‘dog‘,‘cat‘,‘dog‘,‘goldfish‘,‘cat‘,‘rabbit‘,‘cat‘]
while ‘cat‘ in pets:
pets.remove(‘cat‘)
print(pets)
[‘dog‘, ‘dog‘, ‘goldfish‘, ‘rabbit‘]
#使用用户输入来填充字典
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 respond?(yes/no)")
if repeat == ‘no‘:
polling_active = False
What is your name?cui
Which mountain would you like to climb someday?huang
Would you like to let another person respond?(yes/no)yes
What is your name?lu
Which mountain would you like to climb someday?zijin
Would you like to let another person respond?(yes/no)yes
What is your name?ting
Which mountain would you like to climb someday?tai
Would you like to let another person respond?(yes/no)no
#调查结束,显示结果
print("\n--- Poll Results ---")
for name,response in responses.items():
print(name + " would like to climb " + response + ".")
--- Poll Results ---
cui would like to climb huang.
lu would like to climb zijin.
ting would like to climb tai.