python编程 从入门到实践 第七章 字典用户输入和while

prompt="If you tell us who you are ,we can personalize the messages you see."
prompt+="\nwhat is your first name? "
name=input(prompt)
print(f"\nhello,{name}")

输入一个数,返回奇数与偶数

number=input("enter a number, and I will tell you if it`s even or odd: ")
number=int(number)
if number % 2==0:
	print(f"\n{number} is even.")
else:
	print(f"\n{number} is odd.")

#8人以上无座位

number=int(input("how many people?"))
if number > 8 :
	print("sorry,没有空座位!")
else:
	print("hhh,有空位!!!")

使用while输入1-5的数

current_number=1
while current_number<=5:
	print(current_number)
	current_number+=1
#1 2 3 4 5 

返回不能被2整除的数

current_number=0
while current_number<10:
	current_number+=1
	if current_number%2==0:
		continue
	else:
		print(current_number)
 
#1 3 5 7 9

习题7-4

prompt='\n请输入你需要添加的佐料:'
prompt+='\n输入"quit"停止添加\n'
active=True
addings=[]
while active:
	food=input(prompt)
	addings.append(food)
	if food=='quit':
		active=False
	else:
		print(addings)

习题7-5

prompt="How old are you?"
prompt+="\nenter 'quit' 退出 "
active=True
while active:
	age=input(prompt)
	if age=='quit':
		break
	age=int(age)

#删除列表中的特定元素

pets=['dog','cat','dog','fish','cat','rabbit']
print(pets)
while 'cat' in pets:
	pets.remove('cat')
print(pets)
 
#['dog', 'cat', 'dog', 'fish', 'cat', 'rabbit']
#['dog', 'dog', 'fish', 'rabbit']

使用一个列表填充另一个空列表

sandwich_orders=['apple','banana','link','waterlemon','orange']
finished_sanwiches=[]
 
while sandwich_orders:
	print(sandwich_orders)
	sandwiches=sandwich_orders.pop()
	finished_sanwiches.append(sandwiches)
for finished_sanwiche in finished_sanwiches:
	print(finished_sanwiche)
 
"""
['apple', 'banana', 'link', 'waterlemon', 'orange']
['apple', 'banana', 'link', 'waterlemon']
['apple', 'banana', 'link']
['apple', 'banana']
['apple']
orange
waterlemon
link
banana
apple
"""

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值