购物车
li = [ {'name':'苹果','price':10}, {'name':'香蕉','price':20}, {'name':'西瓜','price':30} ] shopping_car = {} print('欢迎光临大铁锤水果店') money = input('让我看看你的钱') while 1: if money.isdigit() and int(money) > 0: for i,k in enumerate(li): print('序号{},商品{},价格{}'.format(i,k['name'],k['price'])) choose = input('请输入您要购买的商品序号') if choose.isdigit() and int(choose) < len(li): num = input('您要购买的商品数量') if num.isdigit(): if int(money) > li[int(choose)]['price'] * int(num): money = int(money) - li[int(choose)]['price'] * int(num) if li[int(choose)]['name'] in shopping_car: shopping_car[li[int(choose)]['name']] = shopping_car[li[int(choose)]['name']] + int(num) else: shopping_car[li[int(choose)]['name']] = int(num) print('购物车中的商品有{},您的余额为{}'.format(shopping_car,money)) else: print('穷鬼,回去跟你老婆要钱!') break else: print('都说了是序号,你傻呀!!')
三次登陆
i = 3 username = "熊熊" password = "520" while i > 0: name = input('请输入你的用户名:') i -= 1 if name == username: word = input('请输入你的密码') if word == password: print('验证成功,正在登陆....') print('''恭喜你登陆成功 欢迎用户进入 用户名 :%s 密码 : %s '''%(name,word)) break else: if i == 0: print('你的机会已经用完了!game over 下次见!') answer = input('再试试?Y or N') if answer == 'Y': i = 3 print('密码错误,请重新输入') print('你还有"+str(i)+"次机会') else : print("请输入正确的用户名!") if i == 0: print('你的机会已经没有了!') answer = input('再试试?Y or N') if answer == 'Y': i = 3 print("你还有"+str(i)+"次机会") else: print('你TM要不要脸')
求和
计算1-2+3....+99中除了88之外所有数的和 i = 0 j = -1 sum = 0 while i < 99: i = i + 1 if i == 88: continue else: j = -j sum = sum + i*j print(sum)