一、用户输入

  1. 函数 .input()
    1. msg = input("Tell me something, and I will repeat it back to you: ")
    2. print(msg)
    :::tips 函数input()接受一个参数;在这个示例中,python运行第一行代码时,用户将看到提示:
    Tell me something, and I will repeat it back to you:
    程序等待用户输入,并在用户按回车键后继续运行。
    输入存储在变量msg中,接下来的print(msg)将输入输出。 :::

当必要的提示不止一行时,我们可以:

  1. #先将部分提示存入一个变量,再使用+=运算符接上剩余的提示
  2. prompt = "If you tell us who you are, we can personalize the messages you see."
  3. prompt += "\nWhat is your first name? "
  4. name = input(prompt)
  5. print("\nHello, " + name + "!")

:::tips output:
If you tell us who you are, we can personalize the messages you see.
What is your first name? Ming

Hello, Ming! :::

  1. 使用 int() 来获取数值输入
    input会将输入理解为字符串,我们需要int来转换。将数值用于计算和比较前,务必将其转化为数值表示,避免不必要的错误。 ```python 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 little older.”)

  1. 3. 求模运算符:% (**同c/c++**)<br />**python 2.7 中应使用: raw_input()来获取输入。**
  2. ---
  3. <a name="35bf65f8"></a>
  4. ## 二、while循环
  5. <a name="3842fb6a"></a>
  6. #### 1、简述:
  7. ```python
  8. prompt = "\nTell me something, and I will repeat it back to you:"
  9. prompt += "\nEnter 'quit' to end the program. "
  10. active = True #随便定义的一个标志变量
  11. while active: #while的用法与c/c++类似,区别是不用括号
  12. message = input(prompt)
  13. if message == 'quit':
  14. active = False
  15. else:
  16. print(message)

break/continue在python中的用法与c/c++中相同。

2、用while循环来处理列表和字典:

  1. 在列表间移动元素: ```python

    首先,创建一个待验证用户列表

    和一个用于存储已验证用户的空列表

    unconfirmed_users = [‘alice’, ‘brian’, ‘candace’] confirmed_users = []

验证每个用户,直到没有未验证用户为止

将每个经过验证的用户都移动到已验证用户列表中

while unconfirmed_users: current_user = unconfirmed_users.pop()

  1. print("Verifying user: " + current_user.title())
  2. confirmed_users.append(current_user)

显示所有已验证用户

print(“\nThe following users have been confirmed:”) for confirmed_user in confirmed_users: print(confirmed_user.title())

  1. :::tips
  2. output:<br />Verifying user: Candace<br />Verifying user: Brian<br />Verifying user: Alice
  3. The following users have been confirmed:<br />Candace<br />Brian<br />Alice
  4. :::
  5. 2. **删除包含特定值的所有列表元素**:
  6. ```python
  7. pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
  8. print(pets)
  9. while 'cat' in pets:
  10. pets.remove('cat')
  11. print(pets)

:::tips output:
[‘dog’, ‘cat’, ‘dog’, ‘goldfish’, ‘cat’, ‘rabbit’, ‘cat’]
[‘dog’, ‘dog’, ‘goldfish’, ‘rabbit’] :::

  1. 使用用户输入来充填字典: ```python responses = {}

设置一个标志,指出调查是否继续

polling_active = True

while polling_active:

  1. # 提示输入被调查者的名字和回答
  2. name = input("\nWhat is your name? ")
  3. response = input("Which mountain would you like to climb someday? ")
  4. # 将答案存储在字典中
  5. responses[name] = response
  6. # 看看是否还有人要参与调查
  7. repeat = input("Would you like to let another person respond? (yes/ no) ")
  8. if repeat == 'no':
  9. polling_active = False

调查结束,显示结果

print(“\n—- Poll Results —-“) for name, response in responses.items(): print(name + “ would like to climb “ + response + “.”) ``` :::tips output:
What is your name? 小明
Which mountain would you like to climb someday? 珠穆朗玛峰
Would you like to let another person respond? (yes/ no) yes

What is your name? 不告诉你
Which mountain would you like to climb someday? 也不告诉你
Would you like to let another person respond? (yes/ no) no

—- Poll Results —-
小明 would like to climb 珠穆朗玛峰.
不告诉你 would like to climb 也不告诉你. :::