比萨配料 :编写一个循环,提示用户输入一系列的比萨配料,并在用户输入'quit' 时结束循环。每当用户输入一种配料后,都打印一条消息,说我们会在比萨 中添加这种配料。
while True:
ingredient=input('请输入比萨配料')
print(f"我们在比萨中添加{ingredient}" )
if ingredient=='quit':
break
'''
请输入比萨配料胡萝卜
我们在比萨中添加胡萝卜
请输入比萨配料西红柿
我们在比萨中添加西红柿
请输入比萨配料鸡蛋粉
我们在比萨中添加鸡蛋粉
请输入比萨配料quit
我们在比萨中添加quit
'''
电影票 :有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用 户的年龄,并指出其票价。
while True:
age=int(input('请输入您的年龄'))
if age>0:
if age<3:
print('免费')
elif 3<=age<=12:
print('票价为10美元')
else:
print('票价为15美元')
break
else:
print('请输入正确的年龄')
三个出口 :以另一种方式完成练习4或练习5,在程序中采取如下所有做法。 在while 循环中使用条件测试来结束循环。 使用变量active 来控制循环结束的时机。 使用break 语句在用户输入'quit' 时退出循环。
active=True
while active:
ingredient=input('请输入比萨配料')
print(f"我们在比萨中添加{ingredient}" )
if ingredient=='quit':
active=False
无限循环 :编写一个没完没了的循环,并运行它(要结束该循环,可按Ctrl +C,也可关闭显示输出的窗口)。
while True:
print('vova')
熟食店 :创建一个名为sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为finished_sandwiches 的空列表。遍历列表sandwich_orders ,对于其中的每种三明治,都打印一条消息,如I made your tuna sandwich ,并将其移到列表finished_sandwiches 。所有三明 治都制作好后,打印一条消息,将这些三明治列出来。
sandwich_orders=['猪爪','肘子','三明治','肠']
finished_sandwiches=[]
for i in sandwich_orders:
print(f"I made your tuna sandwich")
finished_sandwiches.append(i)
print(finished_sandwiches)
'''
I made your tuna sandwich
I made your tuna sandwich
I made your tuna sandwich
I made your tuna sandwich
['猪爪', '肘子', '三明治', '肠']
['猪爪', '肘子', '三明治', '肠']
'''
# 五香烟熏牛肉( pastrami)卖完了 :使用为完成练习8而创建的列表sandwich_orders ,并确保'pastrami'在其中至少出现了三次。在程序开头附近添加这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个while循环将列表sandwich_orders中的'pastrami'都删除。确认最终的列表finished_sandwiches中不包含'pastrami' 。
sandwich_orders=['pastrami','pastrami','猪爪','肘子','pastrami','三明治','肠']
finished_sandwiches=[]
print('五香烟熏牛肉卖完了')
while len(sandwich_orders)>0:
c=sandwich_orders.pop()
if c!='pastrami':
finished_sandwiches.append(c)
print(finished_sandwiches)
#五香烟熏牛肉卖完了
# ['肠', '三明治', '肘子', '猪爪']
梦想的度假胜地 :编写一个程序,调查用户梦想的度假胜地。使用类似于“If you could visit one place in the world, where would you go?”的提示,并编写一个打印调查结果的代码块。
dict1={}
active=True
while active:
iname=input('请输入你的名字')
palce=input('If you could visit one place in the world, where would you go')
dict1[iname]=palce
yes_no=input('是否还有其他人yes or no')
if yes_no=='yes':
active=True
else:
active=False
for key,value in dict1.items():
print(f"{key}:{value}")
综合循环算法练习leecode: 1. 100以内素数之和(素数也叫质数。一个大于1的正整数,如果除了1和它本身以外,不能被其他正整数整除)
sum=0
for i in range(1,101):
if i%1==0 and i%i==0:
sum+=i
print(sum)
#5050
2. 使用*打印一个正方形
for i in range(6):
for j in range(6):
print('*',end=" ")
print(' ')
'''
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
'''
3. 求1000以内水仙花数: 如果一个3位数等于其各位数字的立方和,这个数就是水仙花数
for i in range(100,1000):
a=int(i/100)
b=int(i/10%10)
c = int(i%10)
if a**3+b**3+c**3==i:
print(i)
'''
153
370
371
407
'''
斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13,特别指出:第0项是0,第1项是第一个1。从第三项开始,每一项都等于前两项之和。 list1=[0, 1, 1, 2, 3, 5, 8, 13] 实现输入需要显示多少项数,然后在程序输出相应的斐波那契数列: 比如输入5 输出 : 0,1,1,2,3。(使用一般方式,递归,迭代或生成器三种方式)
冒泡排序以下列表:
方法一:
li = [1, 9, 4, 7, 7, 8, 2, 3, 5, 8, 13, 10, 11]
for i in range(len(li)):
for j in range(len(li)-1):
if li[j]>li[j+1]:
li[j],li[j+1] = li[j+1],li[j]
print(li)
#[1, 2, 3, 4, 5, 7, 7, 8, 8, 9, 10, 11, 13]
方法二:
a=li.sort()
print(li)