第七章 while、input【Python从入门到实践】

我思故我在:

课本重点代码:

#函数input():打印x之后,等待用户的输入一个值
message=input("tell me something:")
print(message)

#打招呼
name=input('Please enter your name:')
print("hello,"+name+"!")

#将这个事先需要打印的x定义出来
prompt='If you tell us who you are,we can print the message.'
prompt+='\nwhat is you first name?'
name=input(prompt)
print("hello,"+name+"!")

#为了之后能够比较大小,输入值要求是整形int型数字
age=input('How old are you?')
age=int(age)        #数字转成整形很有必要
print(age>=18)

height=input('How tall are you?')
height=int(height)
if height>=110:
    print("\nYou are tall enough to ride!")
else:
    print("\nYou are  not tall enough to ride!")

#求余数
print(5%3)
print(5%2)

#奇偶数判断
number=input("Pleaseenter a number me:")
number=int(number)
if number % 2==0:
    print('\n The number '+str(number)+' is even!')
else:
    print('\n The number ' + str(number) + ' is odd!')

#while循环
prompt='Tell me something,and I will repeat it back to you:'
prompt+='\nEnter "quit" to end the program.'
message=''    #很重要,要不就会显示未定义:'message' is not defined
while message!='quit':
    message = input(prompt)
    if message!='quit':
        print(message)

# #用True、Falkse来决定是否退出
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)

#用break来决定是否退出
prompt='Tell me something,and I will repeat it back to you:'
prompt+='\nEnter "quit" to end the program.'
while True:
    city = input(prompt)
    if city== 'quit':
        break;
    else:
        print('\n I want to go to ' + city +'!')
#continue
current_number=0
while current_number<10:
    current_number+=1
    if current_number%2==0:
        continue            #当数为偶数的时候,执行continue,后续的程序不会被执行
    print(current_number)

#从一个列表转到另一个列表
unconfirm_users=['alice','brain','candace']
confirm_users=[]
while unconfirm_users:
    current_user=unconfirm_users.pop()      #pop 默认先弹出列表的最后一个元素
    print('Verifying user:'+current_user.title())
    confirm_users.append(current_user)
print('The following users have been confirmed:')
for confirm_user in confirm_users:
    print(confirm_user.title())

#删除列表中的所有某个元素
pets=['a','b','c','d','a','c']
print(pets)
while 'a' in pets:
    pets.remove('a')
print(pets)

#用户输入字典
responses={}
polling_active=True

while polling_active:
    name=input('\nWhat is your name?')
    response=input('\nWhich mountain would you like to climb ?')
    responses[name]=response
    repeat=input('\nWould you like to let another person responses(yes/no)?')
    if repeat=='no':
        polling_active=False

print('\n-----poll Result----')
for name,response in responses.items():
    print(name+' would like climb '+response+'!')
    print(responses)

课后习题:

# 7-1 汽车租赁:询问用户要租赁什么样的汽车,并打印一条消息,如“Let me see if I can find you a Subaru”。
message=input('请问要租赁什么样的汽车?')
print('Let me see if I can find you a '+message+'.')

# 7-2 餐馆订位 :询问用户有多少人用餐。如果超过 8人,就打印一条消息,指出没有空桌;否则指出有空桌。
number=input('请问有多少人用餐?')
number=int(number)
if(number>8):
    print('\n 没有空桌!')
else:
    print('\n 有空桌!')

# 7-3 10的整数倍 :让用户输入一个数字,并指出这个数字是否是10的整数倍
number=input('请输入一个数字:')
number=int(number)
if(number%10==0):
    print('\n 是10的整数倍!')
else:
    print('\n 不是是10的整数倍!')

#7-4 比萨配料 :编写一个循环,提示用户输入一系列的比萨配料, 并在用户输入'quit' 时结束循环。
# 每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料。
message= ''
while message!='quit':
    message=input('请输入一系列的比萨配料:')
    if message!='quit':
        print(message)

# 7-5 电影票:根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众为10美元;超过12岁的观众为15美元。
# 请编写一个循环,在其中询问用户的年龄,并指出其票价。
while True:
   age=input('请输入年龄:')
   age=int(age)
   if age<3:
       print('The price is free!')
   elif age<=12:
       print('The price is 10$!')
   else:
       print('The price is 15$!')

# 7-6 三个出口 :以另一种方式完成练习7-4或练习7-5,在while 循环中使用条件测试来结束循环。
# 使用变量active来控制循环结束的时机。 使用break 语句在用户输入'quit' 时退出循环。
message= ''
active=True
while active:
    message = input('请输入一系列的比萨配料:')
    if message=='quit':
        active=False
    else:
        print(message)

#7-7 无限循环 :编写一个没完没了的循环,并运行它(要结束该循环,可按Ctrl +C,也可关闭显示输出的窗口)。
while True:
    print('I will become a stronger person!')

# 7-8 熟食店 :创建一个名为sandwich_orders 的列表,在其中包含 各种三明治的名字;再创建一个名为finished_sandwiches 的空列表。
# 遍历列表sandwich_orders,对于其中的每种三明治,都打印 一条消息,如I made your tuna sandwich ,并将其移到列 表finished_sandwiches 。
# 所有三明治都制作好后,打印一条消 息,将这些三明治列出来。
sandwich_orders=['Pork sandwich','Vegetable sandwich','Barbecue sandwich']
finished_sandwiches=[]
for sandwich_order in sandwich_orders:
    current_sandwich=sandwich_order
    finished_sandwiches.append(current_sandwich)
    print('I made your '+sandwich_order+'.')
print(finished_sandwiches)

# 7-9 五香烟熏牛肉(pastrami)卖完了:使用为完成练习7-8而创建的列表sandwich_orders ,并确保'pastrami' 在其中至少出现了 三次。
# 在程序开头附近添加这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个while 循环将列 表sandwich_orders 中的'pastrami' 都删除。
# 确认最终的列 表finished_sandwiches 中不包含'pastrami' 。
print('熟食店的五香烟熏牛肉卖完了.')
sandwich_orders=['pastrami','Pork sandwich','Vegetable sandwich','pastrami' ,'Barbecue sandwich','pastrami' ]
while'pastrami' in sandwich_orders:
     sandwich_orders.remove('pastrami')
print(sandwich_orders)

#7-10 梦想的度假胜地:调查用户梦想的度假胜地。使用类似于“If you could visit one place in the world, where would you go?”的提示,并编写一个打印调查结果的代码块。
prompt='If you could visit one place in the world, where would you go?'
name=input(prompt)
print("You want to go"+name+"!")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值