Python实例1:编写程序打印购物小票

首先录入商品信息,再购买商品,最后打印购物小票

代码:

#录入商品信息
name1 = '苹果'
num1 = '1001'
price1 = 2.5
 
name2 = '香蕉'
num2 = '1002'
price2 = 3.5
 
name3 = '西瓜'
num3 = '1003'
price3 = 19.9

#定义初始值
name = ''
i=1
allnum=0; price=0
count1=0; price11=0
count2=0; price22=0
count3=0; price33=0
while (i==1):
    num = input('请输入商品编号:')
    count = int(input('请输入商品数量:'))
    if num == '1001':
        name = name1
        count1 += count
        price11 = price1*count1
    elif num == '1002':
        name = name2
        count2 += count
        price22 = price2*count2
    elif num == '1003':
        name = name3
        count3 += count
        price33 = price3*count3
    else:
        print('没有此商品!')
    allnum = count1+count2+count3
    price = price11+price22+price33
    i=int(input("如果还有商品请输入1,否则请输入0:"))#注意input输入的默认为字符串,要换算成整数
    
print(price,allnum,count1,count2,count3)
 
if price != 0:
    money = float(input('请输入付款金额:'))
    while money < price:
        print('付款金额不足,请重新付款!')
        money = float(input('请输入付款金额:'))
    RMB = round(money-price,1)
    
    print('\n'+'*'*30)#打印购物小票
    print('单号:xxx')
    print('时间:20xx-xx-xx')
    print('*'*30)
    print('商品\t单价\t数量\t金额')
    if count1 != 0:
            print(f'{name1}\t{price1}\t{count1}\t{price11}')
    if count2 != 0:
            print(f'{name2}\t{price2}\t{count2}\t{price22}')
    if count3 != 0:
            print(f'{name3}\t{price3}\t{count3}\t{price33}')
	    
    print('*'*30)
    print(f'总数:{count}   \t  总额:{price}')
    print(f'实收:{money}\t  找零:{RMB}')
    print('收银:管理员1')
    print('*'*30)

结果为: 

请输入商品编号:1001
请输入商品数量:2
如果还有商品请输入1,否则请输入0:1
请输入商品编号:1003
请输入商品数量:5
如果还有商品请输入1,否则请输入0:1
请输入商品编号:1002
请输入商品数量:2
如果还有商品请输入1,否则请输入0:1
请输入商品编号:1002
请输入商品数量:1
如果还有商品请输入1,否则请输入0:0
115.0 10 2 3 5
请输入付款金额:120

******************************
单号:xxx
时间:20xx-xx-xx
******************************
商品    单价    数量    金额
苹果    2.5     2       5.0
香蕉    3.5     3       10.5
西瓜    19.9    5       99.5
******************************
总数:1            总额:115.0
实收:120.0        找零:5.0
收银:管理员1
******************************

物联沃分享整理
物联沃-IOTWORD物联网 » Python实例1:编写程序打印购物小票

发表评论