-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter7_InputAndWhile.py
175 lines (119 loc) · 3.4 KB
/
Chapter7_InputAndWhile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# message = input("Tell me something, and I will repeat it back to you: ")
# print(message)
#
# name = input("Please enter your name: ")
# print("Hello, " + name + "!")
#
# prompt = "If you tell us who you are, we can personalize the messages you see."
# prompt += "\nWhat is your first name? "
#
# name = input(prompt)
# print("\nHello, " + name + "!")
# age = input("How old are you? ")
# print(age)
#
# #int()函数将字符串转为数值表示
# age = int(age)
# print(age>18)
# height = input("How tall are you, in inches? ")
# height = int(height)
#
# if height >= 36:
# print("\nYou're tell enough to ride! ")
# else:
# print("\nYou'll be able to ride when you're a little older.")
"""
求模运算符 % 取余数
"""
# 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.")
"""
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)
active = True
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print(message)
"""
使用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("I'd love to go to " + city.title() + "!")
"""
continue
"""
current_number = 0
while current_number < 10:
current_number += 1
if current_number%2 == 0:
continue
print(current_number)
"""
使用while循环来处理列表和字典
"""
# 首先创建一个待验证用户列表
# 和一个用于存储已验证用户的空列表
unconfirmed_users = ['alice', 'brain', '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())
"""
删除包含特定值的所有列表元素
"""
pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
"""
使用用户输入来填充字典
"""
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
#调查结束 显示结果
print("\n--- Poll Results ---")
for name, response in responses.items():
print(name + " would like to climb " + response + ".")