目录
7.1 input
input让程序暂停运行,等待用户输入文本,用户输入后将其内容存储在变量中
message=input('tell me something,and i will repeat it back to you: ')
print(message)
tell me something,and i will repeat it back to you: hello
hello
1.编写程序
name=input('please enter your name: ')
print('hello '+name)
please enter your name: eric
hello eric
当提示内容过多时,可使用+=运算符
prompt='if you tell us who you are,we can personalize the message you see.'
prompt+='\nWhat is your first name?'
name=input(prompt)
print('hello '+name)
if you tell us who you are,we can personalize the message you see.
What is your first name?eric
hello eric
2.int数值输入
input输入内容为字符串,当需输入数值时,使用int
>>>age=input('how old are you? ')
age=int(age)
print(age)
how old are you? 23
23
3.求模运算
%将两数相除返回余数
>>> 4%3
1
>>> 5%2
1
>>> 10%3
1
利用该运算符可以判断一个数是奇数还是偶数
number=input("enter a number,i will tell you if it's even or odd: ")
number=int(number)
if number%2==0:
print("this number is even")
else:
print("this number is odd")
7.2 while循环
1.使用while循环
原理与C语言中while循环类似
current_number=1
while current_number<=5:
print(current_number)
current_number+=1
1
2
3
4
5
2.选择退出
message=('tell me something, and i will repeat it back to you: ')
message+="\nEnter 'quit' to end the program. "
key=''
while key !='quit':
key=input(message)
print(key)
#输出
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. nihao
nihao
tell me something, and i will repeat it back to you:
Enter 'quit' to end the program. quit
quit
3.使用标志
实际上,可以结束程序的情况有很多种,但在一条while语句中实现较难
在要求很多条件都满足才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态。这个变量被称为标志。
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)
4.break
可以跳出while循环5.
prompt="\nEnter you favorite city: "
prompt+="\nEnter 'quit' when you are finished. "
while True:
city=input(prompt)
if city=='quit':
break
else:
print("i like "+city)
Enter you favorite city:
Enter 'quit' when you are finished. beijing
i like beijing
Enter you favorite city:
Enter 'quit' when you are finished. quit
5.continue
current_number=0
while current_number<10:
current_number+=1
if current_number%2==0:
continue #返回到循环开头
print(current_number)
1
3
5
7
9
6.避免无限循环
while循环必须设置结束条件,否则将陷入无限循环
x=1
while x<5:
print(x)
7.3 处理列表与字典
1.在列表间移动元素
unconfirmed_users=['curry','james','paul']
confirmed_users=[]
while unconfirmed_users:
current_user=unconfirmed_users.pop() #pop()移除列表元素,并返回该元素值
print("verifying user: "+current_user.title())
confirmed_users.append(current_user)
print("the following users have been confirmed")
for confirmed_user in confirmed_users:
print(confirmed_user.title())
verifying user: Paul
verifying user: James
verifying user: Curry
the following users have been confirmed
Paul
James
Curry
2.删除包含特定值的所有列表元素
remove()可以删除列表中的特定值,但若特定值多次出现,需使用循环
pets=['dog','cat','cat','tigger','bird','cat','monkey']
print(pets)
while 'cat' in pets:
pets.remove('cat')
['dog', 'cat', 'cat', 'tigger', 'bird', 'cat', 'monkey']
['dog', 'tigger', 'bird', 'monkey']
3.使用用户输入填充字典
responses={}
active=True
while active:
name=input("\nWhat's your name? ")
response=input("do you like drinking? ")
responses[name]=response
repeat=input("\nAre there any questions left?(yes/no) ")
if repeat=='no':
active=False
print("--survey results--")
for name,response in responses.items():
print(name+' say: '+response)
What's your name? kite
do you like drinking? yes
Are there any questions left?(yes/no) yes
What's your name? pete
do you like drinking? no
Are there any questions left?(yes/no) yes
What's your name? cun
do you like drinking? no
Are there any questions left?(yes/no) no
--survey results--
kite say: yes
pete say: no
cun say: no
注:Python注释无法使用汉字,在文件开头写上如下代码可解决
# -- coding: utf-8 --
作业
car=input("What kind of car do you want? ")
print("let me see if i can find you a "+car)
#结果
#What kind of car do you want? subaru
#let me see if i can find you a subaru
2.编写一个程序,询问用户有多少人用餐。如果超过8人,就打印一条消息,指出没有空桌;否则指出有空桌。
number=input("How many people are there for?")
number=int(number)
if number > 8:
print("\nWe don't have any vacant seats")
else:
print("\nAll right, I have a spare")
3.让用户输入一个数字,并指出这个数字是否是10的整数倍。
number=input('please enter a number: ')
number=int(number)
if number % 10==0:
print("This number is an integer multiple of 10")
else:
print("This number isn't an integer multiple of 10")
4.编写一个循环,提示用户输入一系列的比萨配料,并在用户输入'quit' 时结束循环。每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料。
prompt="What ingredients are needed?"
active=True
while active:
pizza=input(prompt)
if pizza=='quit':
active=False
else:
print("\nWe're going to add this ingredient")
print("\n")
5.有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用户的年龄,并指出其票价。
prompt="How old are you?"
while True:
age=input(prompt)
age=int(age)
if age <3:
print("free")
elif age <12:
print("10 yuan")
else:
print("15 yuan")
print("\n")
6.编写一个没完没了的循环,并运行它
number=12
while True:
number+=1
print(number)
7.创建一个名为sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为finished_sandwiches 的空列表。遍历列表sandwich_orders ,对于其中的每种三明治,都打印一条消息,如I made your tuna sandwich ,并将其移到列表finished_sandwiches 。所有三明治都制作好后,打印一条消息,将这些三明治列出来。
sandwich_orders=['beef','chicken','tomato']
finished_sandwiches=[]
while sandwich_orders:
making_sandwich=sandwich_orders.pop()
print("i made your "+making_sandwich+" sandwich\n")
finished_sandwiches.append(making_sandwich)
print(finished_sandwiches)
8.使用为完成练习7-8而创建的列表sandwich_orders ,并确保'pastrami' 在其中至少出现了三次。在程序开头附近添加这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个while 循环将列表sandwich_orders 中的'pastrami' 都删除。确认最终的列表finished_sandwiches 中不包含'pastrami'
sandwich_orders=['beef','pastrami','chicken','tomato','pastrami']
finished_sandwiches=[]
print(sandwich_orders)
print("we have not pastrami sandwich\n")
while sandwich_orders:
making_sandwich=sandwich_orders.pop()
if making_sandwich=='pastrami':
sandwich_orders.remove('pastrami')
else:
print("i made your "+making_sandwich+" sandwich\n")
finished_sandwiches.append(making_sandwich)
print(finished_sandwiches)
#['beef', 'pastrami', 'chicken', 'tomato', 'pastrami']
#we have not pastrami sandwich
#i made your tomato sandwich
#i made your beef sandwich
#['tomato', 'chicken', 'beef']
9.编写一个程序,调查用户梦想的度假胜地。使用类似于“If you could visit one placein the world, where would you go?”的提示,并编写一个打印调查结果的代码块。
question="can i ask your a question?"
answer={}
active=True
while active:
decison=input(question)
if decison=='yes':
name=input("what's your name?\n")
place=input("If you could visit one placein the world, where would you go?\n")
answer[name]=place
else:
active=False
print("\n")
for key,value in answer.items():
print(key+" like "+value)