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)
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. nice to meet you
nice to meet you
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. quit
prompt ="\nTell me somthing, and I will repeat it back to you: "
prompt +="\nEnter 'quit' to end the program."
active =Truewhile active:
message =input(prompt)if message =='quit':
active =Falseelse:print(message)
Tell me somthing, and I will repeat it back to you:
Enter 'quit' to end the program.hi
hi
Tell me somthing, and I will repeat it back to you:
Enter 'quit' to end the program.quit
使用break退出循环
要立即退出 while 循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用 break 语句
break 语句用于控制程序流程,可使用它来控制哪些代码行将执行,哪些代码行不执行
在任何 Python 循环中都可使用 break 语句
prompt ="\nTell me somthing, and I will repeat it back to you: "
prompt +="\nEnter 'quit' to end the program."
active =Truewhile active:
message =input(prompt)if message =='quit':breakelse:print(message)
Tell me somthing, and I will repeat it back to you:
Enter 'quit' to end the program.hi
hi
Tell me somthing, and I will repeat it back to you:
Enter 'quit' to end the program.quit
responses ={}# 设置一个标志,指出调查是否继续
polling_active =Truewhile 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.title()+" would like to climb "+ response.title()+".")
What is your name? yegeli
Which mountain would you like to climb someday? tanshang
Would you like to let another person respond? (yes/ no) yes
What is your name? ww
Which mountain would you like to climb someday? furongshan
Would you like to let another person respond? (yes/ no) no
--- Poll Results ---
Yegeli would like to climb Tanshang.
Ww would like to climb Furongshan.
练习
# 1.熟食店# 创建一个名为 sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为 finished_sandwiches 的空列表。遍历列# 表 sandwich_orders ,对于其中的每种三明治,都打印一条消息,如 I made your tuna sandwich ,并将其移到列表 finished_sandwiches 。# 所有三明治都制作好后,打印一条消息,将这些三明治列出来。
sandwich_orders =['name1','name2','name3']
finished_sandwiches =[]while sandwich_orders:
sandwich = sandwich_orders.pop()print("I made your tuna "+ sandwich.title()+" .")
finished_sandwiches.append(sandwich)print("sandwiches is finished! ")for finish_sandwich in finished_sandwiches:print(finish_sandwich)
I made your tuna Name3 .
I made your tuna Name2 .
I made your tuna Name1 .
sandwiches is finished!
name3
name2
name1
# 2.五香烟熏牛肉pastrami卖完了# 使用为完成练习 1而创建的列表 sandwich_orders ,并确保 'pastrami' 在其中至少出现# 了三次。在程序开头附近添加这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;# 再使用一个 while 循环将列表 sandwich_orders 中的 'pastrami' 都删除。确认最终的列表# finished_sandwiches 中不包含 'pastrami'
sandwich_orders =['name1','pastrami','name2','pastrami','name3','pastrami']
finished_sandwiches =[]print("The pastrami is over! ")while'pastrami'in sandwich_orders:
sandwich_orders.remove('pastrami')print(sandwich_orders)while sandwich_orders:
sandwich = sandwich_orders.pop()print("I made your "+ sandwich.title()+" .")
finished_sandwiches.append(sandwich)print("sandwiches is finished! ")for finish_sandwich in finished_sandwiches:print(finish_sandwich)
The pastrami is over!
['name1', 'name2', 'name3']
I made your Name3 .
I made your Name2 .
I made your Name1 .
sandwiches is finished!
name3
name2
name1
# 3.梦想的度假胜地:# 编写一个程序,调查用户梦想的度假胜地。使用类似于 “If you could visit one place in the world, where would you go?” # 的提示,并编写一个打印调查结果的代码块。
holidays ={}
active =Truewhile active:
name =input("请输入您的姓名: ")
address =input("请输入您想去的地方:")
holidays[name]= address
other_people =input("是否要继续输入(是/否):")print("\n")if other_people =="否":
active =Falseelse:continueprint("-----The Result----")for name,address in holidays.items():print(name +" ,想去"+ address +"度假!")