Skip to content

Commit 40fa31b

Browse files
author
VincentJ
committed
[C]增加了用户输入和while的一些使用以及while在列表和字典中的使用
1 parent b61e7dc commit 40fa31b

File tree

7 files changed

+123
-0
lines changed

7 files changed

+123
-0
lines changed

chapter6/Break.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 使用break跳出循环
2+
3+
prompt = "\nPlease enter the name of a city you have visited:"
4+
prompt += "\n(Enter 'quit' when you are finished.) "
5+
while True:
6+
city = input(prompt)
7+
if city != 'quit':
8+
print("I'd love to go to " + city.title() + "!")
9+
else:
10+
break

chapter6/Continue.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 使用continue
2+
num = 0
3+
while num < 10:
4+
num += 1
5+
if num % 2 == 0:
6+
continue
7+
else:
8+
print(num)

chapter6/Input.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# input当做字符串来处理
2+
3+
message = input("Tell me something , I will repeat it to U \n")
4+
print(message)
5+
6+
name = input("Please input your name:\n")
7+
print("Hello " + name + " !")
8+
9+
# 编写清晰的程序--必要的换行处理
10+
prompt = "If you tell us who you are, we can personalize the messages you see."
11+
prompt += "\nWhat is your first name? "
12+
name = input(prompt)
13+
print("\nHello, " + name + "!")
14+
15+
# int来处理字符串当做数值
16+
17+
age = input("How old are U ?")
18+
# 这里是字符串
19+
print(age)
20+
# print(age > 10)
21+
# 这才是数字
22+
print(int(age))
23+
24+
height = input("How tall are you, in inches? ")
25+
height = int(height)
26+
if height >= 36:
27+
print("\nYou're tall enough to ride!")
28+
else:
29+
print("\nYou'll be able to ride when you're a little older.")

chapter6/Mod.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 取模运算
2+
print(4 % 3)
3+
print(4 % -3)
4+
5+
number = input("Enter a number, and I'll tell you if it's even or odd: ")
6+
number = int(number)
7+
if number % 2 == 0:
8+
print("\nThe number " + str(number) + " is even.")
9+
else:
10+
print("\nThe number " + str(number) + " is odd.")

chapter6/Parrot.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 使用标识位结束
2+
3+
prompt = "\nTell me something, and I will repeat it back to you:"
4+
prompt += "\nEnter 'quit' to end the program. "
5+
active = True
6+
7+
while active:
8+
message = input(prompt)
9+
if message != 'quit':
10+
print(message)
11+
else:
12+
active = True

chapter6/WhileOperation.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
num = 0
2+
while num <= 5:
3+
print(num)
4+
num += 1
5+
# 让用户决定什么时候结束程序的运行
6+
prompt = "\nTell me something, and I will repeat it back to you:"
7+
prompt += "\nEnter 'quit' to end the program. "
8+
message = ""
9+
while message != 'quit':
10+
message = input(prompt)
11+
if message != 'quit':
12+
print(message)

chapter6/confirmed_users.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 使用While来处理列表和字典
2+
3+
# 首先, 创建一个待验证用户列表
4+
# 和一个用于存储已验证用户的空列表
5+
unconfirmed_users = ['alice', 'brian', 'candace']
6+
confirmed_users = []
7+
# 验证每个用户, 直到没有未验证用户为止
8+
# 将每个经过验证的列表都移到已验证用户列表中
9+
while unconfirmed_users:
10+
current_user = unconfirmed_users.pop()
11+
print("Verifying user: " + current_user.title())
12+
confirmed_users.append(current_user)
13+
# 显示所有已验证的用户
14+
print("\nThe following users have been confirmed:")
15+
for confirmed_user in confirmed_users:
16+
print(confirmed_user.title())
17+
18+
# 删除包含特定值的所有列表元素
19+
pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
20+
print(pets)
21+
while 'cat' in pets:
22+
pets.remove('cat')
23+
print(pets)
24+
25+
# 使用用户输入来填充字典
26+
responses = {}
27+
# 设置一个标志, 指出调查是否继续
28+
polling_active = True
29+
while polling_active:
30+
# 提示输入被调查者的名字和回答
31+
name = input("\nWhat is your name? ")
32+
response = input("Which mountain would you like to climb someday? ")
33+
# 将答卷存储在字典中
34+
responses[name] = response
35+
# 看看是否还有人要参与调查
36+
repeat = input("Would you like to let another person respond? (yes/ no) ")
37+
if repeat == 'no':
38+
polling_active = False
39+
# 调查结束, 显示结果
40+
print("\n--- Poll Results ---")
41+
for name, response in responses.items():
42+
print(name + " would like to climb " + response + ".")

0 commit comments

Comments
 (0)