chapter7 用户输入和while循环

在这里插入图片描述

  • input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中
  • 使用函数input(),Python将用户输入解读为字符串
  • 函数int()将数字的字符串表示转换为数值表示
  • 求模运算符%将两个数相处并返回余数
  • break语句:立即退出while循环,不再运行循环中余下的代码,也不管条件测试结果如何
  • continue语句:返回到循环开头,并根据条件测试结果决定是否继续执行循环
  • for循环无法修改列表,使用while循环处理列表和字典

课后习题

在这里插入图片描述

message = input("What kind of car would you like to rent?")
print("Let me see if i can find you a "+message)
--------------------------------------------------------------------
What kind of car would you like to rent?Subaru
Let me see if i can find you a Subaru

在这里插入图片描述

message = input("How many peopele eat?")
if int(message) > 8:
    print("There is no empty table.")
else:
    print("Have table.")
-------------------------------------------------------------
How many peopele eat?10
There is no empty table.

在这里插入图片描述

message = input("Please enter a number:")
if int(message) % 10 == 0:
    print("This number is an integer of 10.")
else:
    print("This number is not an integer of 10.")
-----------------------------------------------------------
Please enter a number:20
This number is an integer of 10.

在这里插入图片描述

pizzas = "\n请输入一系列的披萨配料:"
message = " "

while message != 'quit':
    message = input(pizzas)
    print("\n我们会在披萨中添加"+message)
-----------------------------------------------------
请输入一系列的披萨配料:蘑菇辣椒

我们会在披萨中添加蘑菇辣椒

请输入一系列的披萨配料:quit

我们会在披萨中添加quit

进程已结束,退出代码 0

在这里插入图片描述

message = "\nPlease tell me your age:"
message += "\nEnter 'quit' to end the program."

active = True
while active:#使用标志
    age = input(message)

    if age == 'quit':
        active == False
    elif int(age) < 3:
        print(age+",免费!")
    elif 3<=int(age)<=12:
        print(age+",10美元!")
    elif 12<int(age):
        print(age+",15美元!")
-----------------------------------------------------------------

Please tell me your age:
Enter 'quit' to end the program.2
2,免费!

Please tell me your age:
Enter 'quit' to end the program.6
6,10美元!

Please tell me your age:
Enter 'quit' to end the program.18
18,15美元!

Please tell me your age:
Enter 'quit' to end the program.quit

Please tell me your age:
Enter 'quit' to end the program.

在这里插入图片描述

pizzas = "\n请输入一系列的披萨配料:"
message = " "

active = True
while active:
    message = input(pizzas)
    if message == 'quit':
        break
    else:
        print("\n我们会在披萨中添加"+message+".")
----------------------------------------------------------------
请输入一系列的披萨配料:蘑菇,辣椒

我们会在披萨中添加蘑菇,辣椒.

请输入一系列的披萨配料:quit

进程已结束,退出代码 0


在这里插入图片描述

age = 1
while age <= 3:
    print(age)
--------------------------------------------
1
1
1
1
1
...

在这里插入图片描述

sandwich_orders = ['a','b','c']
finished_sandwiches = []

for sandwich_order in sandwich_orders:
    print("I made your "+sandwich_order+" sandwich.")


while sandwich_orders:
    unfinished_sandwiches = sandwich_orders.pop()
    finished_sandwiches.append(unfinished_sandwiches)

print("\n 所有三明治度都做好了:")
for finished_sandwiche in finished_sandwiches:
    print(finished_sandwiche.title())
-------------------------------------------------------------------------
I made your a sandwich.
I made your b sandwich.
I made your c sandwich.

 所有三明治度都做好了:
C
B
A

在这里插入图片描述

sandwich_orders = ['pastrami','a','pastrami','b','c','pastrami']
finished_sandwiches = []

for sandwich_order in sandwich_orders:
    print(sandwich_order+" sandwich.")


print("熟食店的五香牛肉买完了!\n")
while 'pastrami' in sandwich_orders:
    sandwich_orders.remove('pastrami')

for sandwich_order in sandwich_orders:
    print("We have "+sandwich_order+" sandwich.")


while sandwich_orders:
    unfinished_sandwiches = sandwich_orders.pop()
    finished_sandwiches.append(unfinished_sandwiches)

print("\n 所有三明治度都做好了:")
for finished_sandwiche in finished_sandwiches:
    print(finished_sandwiche.title())
----------------------------------------------------------------------
pastrami sandwich.
a sandwich.
pastrami sandwich.
b sandwich.
c sandwich.
pastrami sandwich.
熟食店的五香牛肉买完了!

We have a sandwich.
We have b sandwich.
We have c sandwich.

 所有三明治度都做好了:
C
B
A

在这里插入图片描述

while True:
    name = input("请输入你的姓名:")
    address = input("你想要去世界上的哪个地方:")
    if address == 'quit' or name == 'quit':
        break
    else:
        print("%s 想要去%s 旅行 " % (name, address))
------------------------------------------------------------------------------
请输入你的姓名:w
你想要去世界上的哪个地方:ha
w 想要去ha 旅行 
请输入你的姓名:quit
你想要去世界上的哪个地方:quit
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w要变强

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值