- 函数input()接受一个参数:即要向用户显示的提示或说明,让用户知道如何做。
- 如果想要指出获取特定输入的原因,可以将提示存储在一个变量中,再将变量传递给函数input().
promt= " what is your first name?"
name = input(promt)
print("hello, "+name)
what is your first name?zhang
hello, zhang
1.使用int()来获取数值输入
- 使用函数input(),python将用户输入解读为字符串类型。
- 使用int(),让python将输入视为数值。
height=input("how tall are you, in inches?")
height=int(height)
if height>=36:
print("\n you're tall enough to ride.")
else:
print("\n you'll be able to ride when you're little older.")
how tall are you, in inches?30
you'll be able to ride when you're little older.
2.求模运算符
- %,将两个数相除并返回余数
判断一个数是奇数还是偶数。
number=input("please input a number: ")
number=int(number)
if number % 2 == 0:
print("the number " + str(number) +" is even")
else:
print("the number "+ str(number) + " is odd.")
please input a number: 33
the number 33is odd.
while循环
1.让用户选择何时退出
- 运算符+= 在存储prompt的字符串末尾附加一个字符串。
prompt = "Tell 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:abc
abc
Tell me something,and i will repeat it back to you
Enter 'quit' to end the program:quit
变量message存储用户输入的值。使用了if条件来进行测试。现在程序在显示消息前将做简单的检查,仅在消息不是退出时才打印它。
2.标志
- 可定义一个变量,用于判断整个程序是否处于活动状态,这个变量被称为标志。
prompt = "Tell 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)
变量active设置为True,让程序最初是处于活动状态。这样简化while语句,因为不需要在其中做任何比较——相关的逻辑由程序的其他部分处理。
3.使用break退出循环
- 要立即退出while循环,不再运行循环中余下的代码 ,也不管条件测试的结果如何,可使用break语句。
prompt = "Please enter the name of city you have visited"
prompt += "\nEnter 'quit' to end the program:"
while True:
city=input(prompt)
if city == 'quit':
break
else:
print(city)
4.在循环中使用continue
- 要返回到循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue语句。
从1数到10,并只打印其中奇数的循环:
current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue
print(current_number)
使用while循环来处理列表和字典
- for循环是一种遍历列表的有效方式,但在for循环中不应修改列表。
- 要在遍历列表的时候同时对其进行修改,可以使用while循环。
1.在列表之间移动元素
- 验证用户后,将他们移动到另一个已验证用户列表。
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())
verifying user: Candace
verifying user: Brian
verifying user: Alice
the following users have been confirmed:
Candace
Brian
Alice
while循环将不断运行,直到列表unconfirmed_users变空。pop()方法以每次一个的方式从列表unconfirmed_users末尾删除未验证的用户。
2.删除包含特定值的所有列表元素
- remove()方法可以删除列表中的特定值,这是因为要删除的值在列表中只出现一次。
- 如果要删除的列表中所有包含特定值的元素,可以不断运行一个while循环。
pets=['dog','cat','dog','goldfish','rabbit','cat','cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
['dog', 'cat', 'dog', 'goldfish', 'rabbit', 'cat', 'cat']
['dog', 'dog', 'goldfish', 'rabbit']
3.使用用户输入来填充字典
- 可以使用while循环来提示用户输入任意数量的信息。
responses={}
pooling_active=True
while pooling_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':
pooling_active=False
print("\n---Poll Results---")
for name,response in responses.items():
print(name+" would you like to climb " + response +".")
what is your name?zhang
which mountain would you like to climb someday?taishan
would you like to let another person respond?(yes/no)yes
what is your name?zhao
which mountain would you like to climb someday?taishan
would you like to let another person respond?(yes/no)no
---Poll Results---
zhang would you like to climb taishan.
zhao would you like to climb taishan.
定义一个空字典,设置一个标志,用来指出调查是否继续。