返回顶部
扩大
缩小

Heaton

python基础(一)demo 三国演义大特购

效果

输错要提示循环操作

代码

"""
三国演义大特购
@author Heaton
@email   70416450@qq.com
"""
def from_data():
    '''
    数据集
    :return: 数据集
    '''
    goods_dict = {
        "001": {"name": "方天画戟", "price": 99998},
        "002": {"name": "青龙偃月刀", "price": 9998},
        "003": {"name": "双股剑", "price": 4998},
        "004": {"name": "丈八蛇矛", "price": 6668},
        "005": {"name": "龙胆枪", "price": 998},
        "006": {"name": "作弊武器-倚天屠龙干将莫邪", "price": 998},
        "007": {"name": "作倚天安徽师大速度", "price": 998}
    }
    return goods_dict

def str_format(str, total_ength=10, if_center=False):
    '''
    格式化数据
    :param str: 传入字符
    :param if_center: 是否居中
    :param total_ength: 总字符长度
    :return: 格式化字符串
    '''
    # 字符长度和总长度差
    len_difference = total_ength - len(str)
    if len_difference > 0:
        if if_center == True:
            half = int(len_difference / 2)
            pre = half * " "
            suf = int(len_difference - half) * " "
            result = pre + str + suf
        else:
            result = str + int(len_difference) * "  "
    else:
        result = str[:total_ength - 1] + "..."
    return result

def if_flag(str, str_type="str"):
    '''
    输入值是否合法。字符Q值为False
    :param str:         输入的值
    :param str_type:    类型
    :return:            Boolean
    '''
    try:
        if str_type == "str" and type(str) == type("string"):
            if str.lower() == "q":
                return False
            return True
        if str_type == "int" and type(int(str)) == type(1):
            return True
        else:
            raise RuntimeError
    except Exception:
        print("您的输入不合法,请重新输入")

def input_num(strdesc='购买的数量:', right_zero=True):
    '''
    获取输入结果数字,默认大于0
    :param strdesc: 内容
    :param right_zero: 是否大于0
    :return:
    '''
    flag = False
    while not flag:
        num = input("………………………………请输入你要" + strdesc)
        flag = if_flag(num, 'int')
        if flag:
            if right_zero and int(num) > 0:
                return int(num)
            else:
                print("必须输入大于0的整数")
                flag = False
                continue
            return int(num)
        continue

def getnum(from_data):
    '''
    返回购买装备编号
    :param from_data: 数据集
    :return:          装备编号
    '''
    # 存储原始装备编号
    numlist = []
    print(str_format("编号", 9), str_format("武器名", 9), str_format("价格", 9))
    for goodkey, goodvalue in from_data.items():
        numlist.append(goodkey)
        print(str_format(goodkey), str_format(goodvalue["name"]), str_format(str(goodvalue["price"])))
    num = input("……………………请选择你要的装备编号:")
    if_flag(num)
    if num not in numlist:
        print("############没有该编号###########")
        return getnum(from_data)
    else:
        return num
    
# 主函数
def main():
    fd = from_data()
    print("……………………欢迎来到三国世界……………………")
    goodlist = []
    buylist = []
    carnum = 0
    flag = True
    while flag:
        goodnum = getnum(fd)
        num = input_num("购买的数量:")
        if goodnum not in goodlist:
            goodlist.append(goodnum)
            buylist.append(num)
        else:
            for x in range(len(goodlist)):
                if goodlist[x] == goodnum:
                    buylist[x] = buylist[x] + num
        carnum += num
        print("购物车物品数量为:",carnum,"个")
        ifq = input("是否继续购物?(q是退出,其它为继续):")
        if ifq is "q" or ifq is "Q":
            flag = False

    sum = 0
    for index in range(len(goodlist)):
        good = fd[goodlist[index]]
        buynum = buylist[index]
        onesum = int(good["price"]) * buynum
        sum += onesum
        print("购买武器:{},数量:{},单价:{},总价:{}".format(good["name"], buynum, good["price"], onesum))
    print("一共需要支付{}钱!!!".format(sum))
    flag2 = True
    while flag2:
        paymoney = input_num('支付的金额:')
        if paymoney >= sum:
            flag2 = False
            print("找您%d元" % (paymoney - sum))
        else:
            print("金额不够")

if __name__ == '__main__':
    main()

posted on 2019-02-14 23:06  咘雷扎克  阅读(435)  评论(0编辑  收藏  举报

导航