1.打印9*9乘法表
for i in range(1, 10):
for j in range(1, 10):
print(j, "x", i, "=", i * j, "\t", end="")
if i == j:
print()
break
for i in range(1,10):
for j in range(1,i+1):
print('%s*%s=%s'%(i,j,i*j),end=' ')
print()
print ("\n".join("\t".join(["%s*%s=%s" %(x,y,x*y) \
for y in range(1, x+1)]) for x in range(1, 10)))
2.# 要求用户输入总资产,例如:2000 显示商品列表,让用户根据序号选择商品,
# 加入购物车 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
shopping = []
salary = input("your salary:").strip()
if salary.isdigit():
salary = int(salary)
print("以下是商品列表:")
for index, item in enumerate(goods):
print(index, item['name'], item['price'])
while True:
choice = input('what you want to buy:').strip()
choice = int(choice)
if (choice >= 0) and (choice < len(goods)):
salary -= int(goods[choice]['price'])
shopping.append((goods[choice]['name']))
print('你所购买商品:%s' % goods[choice]['name'])
print('你所剩余额:%s' % salary)
if salary < int(goods[choice]['price']):
print('你的余额已经不足以购买该商品')
print('一下是你所购买的商品清单:', '\n', shopping)
exit()
else:
continue
else:
print('你输入有误,请重新输入选择序号')
else:
print('你输入有误,请重新输入选择序号')
3.斐波那契数列各种写法:
i, j = 0, 1 while i < 10000: print(i,end='\t') i, j = j, i+j
fibs = [0,1] for i in range(100): fibs.append(fibs[-1] + fibs[-2]) print(fibs)
class Fib:
def __init__(self,n = 10):
self.a = 0
self.b = 1
self.n = n
def __iter__(self):
return self
def __next__(self):
self.a, self.b = self.b, self.a + self.b
if self.a > self.n:
raise StopIteration
return self.a
f = Fib(100)
for i in f:
print(i)
4.三级菜单
dic = {
"河北":{
"石家庄" :{'bei':66},
"邯郸" :{}
},
"河南":{
"郑州":{},
"开封":{}
}
}
lis = []
while True:
for k in dic:
print(k)
choice = input('>>>')
if not choice:continue
if choice in dic:
lis.append(dic)
dic = dic[choice]
elif choice == 'q':
if len(lis) != 0:
dic = lis.pop()
else:
print('顶层啦')