enumerate,‘.format'售货机的用法

enumerate的用法:
vt. 列举;枚举;计算
过去式 enumerated过去分词 enumerated现在分词 enumerating。

程序输入:
lst =[1,2,3,‘d’,‘w’]
for i in enumerate (lst ,start = 1):
print (i)
打印一下:在这里插入图片描述

.format的用法:`在这里插入代码片:
相对基本格式化输出采用‘%’的方法,format()功能更强大,该函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号‘{}’作为特殊字符代替‘%
1.for循环:
for i in range (1,10):
for j in range(i ,i+1):
print(’{}{}={}’.format(j,i,ji),end=’\t’)
print() #j为列 i为行

2.while循环:
row= 1
while row <=9:
col = 1
while col <= row:
print(’{}{}={}’.format(col,row,colrow),end=’\t’)
col += 1
print() #row为行 col为列
row+= 1
打印一下:

售货机的程序:
#使用列表来保存商品
lst=[‘电脑’,‘键盘’,‘鼠标’,‘手机’]
ret=lst.index(‘手机’)
print(ret)

#展示所有商品

for product in lst: #根据内容取索引值
msg = “商品序号:{},名称:{}”.format(lst.index(product)+1,product)
print(msg)

for index ,product in enumerate(lst,start=1):

msg=“商品的序号:{},名称:{}”.format(index,product)

print(msg)

#连续输出
while True:
value = input(“请输入你要买的序号【按Q/q键退出程序】:”)
if value.upper() == ‘Q’:
print(‘程序结束。。。’)
break
elif value.isdigit():
num = int(value)
if num >0 and num <=len(lst):
print(lst[num-1])
else:
print(‘输入的序号有误,请重新输入’)
else:
print(‘输入的内容不正确,请重新输入’)

猜你喜欢

转载自blog.csdn.net/weixin_44252536/article/details/85446691