Python入门试题

0x0编写登陆接口

  • 输入用户名密码
  • 认证成功后显示欢迎信息
  • 输错三次后锁定

解答:

#_*_coding:utf-8_*_
#Author:ymxowgk
name_true = "gaojihe"
password_true = "1234"
name = input("Please input your name:")
if name_true == name:
    i = 0
    while i < 3:
        password = input("Please input your password:")
        if password_true == password:
            print("%s 登陆成功!"%name)
            break
        else:
            i += 1
            print("密码错误,请重新输入")
    else:
        print("密码输入错误次数超过3次,账户已被冻结")
else:
    print("请检查用户名是否输入错误")

0x1购物车程序

 

  • 启动程序之后,让用户输入工资,然后打印商品列表
  • 允许用户根据商品编号购买商品
  • 用户选择商品后,检测余额是否足够,够就直接扣款,不够则提醒
  • 可随时退出,退出时,打印已购买商品和余额
#_*_coding:utf-8_*_
#Author:ymxowgk

'''
购物车
需求:
1、启动程序后,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够则提醒
4、可随时退出,退出时,打印已购买商品和余额
'''

product_list = [
    ('Iphone',5800),
    ('Mac Pro',9800),
    ('Bike',800),
    ('Watch',100050),
    ('Book',100)
]
shopping_list = []
salary = input("Plese input your salary:")
if salary.isdigit():
    salary = int(salary)
    while True:
        for index,item in enumerate(product_list):
            print(index,item)
        user_choice = input("选择你要购买的商品编号\n 'q'退出")
        if user_choice.isdigit():
            user_choice = int(user_choice)
            if user_choice < len(product_list) and user_choice >= 0:
                p_item = product_list[user_choice]
                if p_item[1] <= salary:#买得起
                    shopping_list.append(p_item)
                    salary -= p_item[1]
                    print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m" % (p_item,salary))
                else:
                    print("\033[41;1m你的余额只剩下[%s]啦,还买个毛线\033[0m" % salary)
            else:
                print("product code [%s] is not exist!" % user_choice)
        elif user_choice == 'q':
            print("----shopping list----")
            for p in shopping_list:
                print(p)
            print("your current balance:",salary)
            exit()
        else:
            print("invalid option")

 

    原文作者:ymxowgk
    原文地址: https://blog.csdn.net/qq_35428201/article/details/81464155
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞