学习Python之练习题——快递价格

学习Python之练习题——快递价格
题目要求:
请编写程序,设计一个快递员计算器。规则为:

首重3公斤,未超过3公斤:
其他地区,10元
东三省、宁夏、青海、海南,12元
新疆、西藏,20元
港澳台地区、国外,不接受寄件

超过3公斤每公斤加价:
其他地区,5元/公斤
东三省、宁夏、青海、海南,10元/公斤
新疆、西藏,15元/公斤
港澳台地区、国外,联系总公司

代码:

class Cost(object):
    def __init__(self, weight, uint_price, first_price):
        self.weight = weight
        self.first_prcie = first_price
        self.uint_price = uint_price

    def pay(self):
        if self.weight <= 3:
            t = self.first_prcie
        else:
            t = self.first_prcie + (self.weight - 3)*self.uint_price
        return t

class Area1(Cost):
    def __init__(self, weight, uint_price, first_price):
        Cost.__init__(self, weight, uint_price, first_price)
        self.first_prcie = 10
        self.uint_price = 5

class Area2(Cost):
    def __init__(self, weight, uint_price, first_price):
        Cost.__init__(self, weight, uint_price, first_price)
        self.first_prcie = 12
        self.uint_price = 10

class Area3(Cost):
    def __init__(self, weight, uint_price, first_price):
        Cost.__init__(self, weight, uint_price, first_price)
        self.first_prcie = 20
        self.uint_price = 15

class Area4(Cost):
    def __init__(self, weight, uint_price, first_price):
        Cost.__init__(self, weight, uint_price, first_price)
    def pay(self):
        if self.weight <= 3:
            print("不接受寄件!")
        else:
            print('请联系公司!')

y = 0
z = 0
print("地区1:其他地区\n地区2:东三省、宁夏、青海、海南\n地区3:新疆、西藏\n地区4:港澳台地区、国外")

while True:
    x = input('\n请输入地区:')
    x = int(x)
    if x == 1:
        print('你所选地区为:其他地区')
        while True:
            y = input('\n请输入重量:')
            y = int(y)
            if y <= 0:
                print('请输入正确数值!')
            if y > 0:
                break
        tt = Area1(y, 5, 10)
        t = tt.pay()
        print('所需总价为:%s'%(t))

    elif x == 2:
        print('你所选地区为:东三省、宁夏、青海、海南')
        while True:
            y = input('\n请输入重量:')
            y = int(y)
            if y <= 0:
                print('请输入正确数值!')
            if y > 0:
                break
        tt = Area2(y, 10, 12)
        t = tt.pay()
        print('所需总价为:%s'%(t))

    elif x == 3:
        print('你所选地区为:新疆、西藏')
        while True:
            y = input('\n请输入重量:')
            y = int(y)
            if y <= 0:
                print('请输入正确数值!')
            if y > 0:
                break
        tt = Area3(y, 15, 20)
        t = tt.pay()
        print('所需总价为:%s'%(t))

    elif x == 4:
        print('你所选地区为:港澳台地区、国外')
        while True:
            y = input('\n请输入重量:')
            y = int(y)
            if y <= 0:
                print('请输入正确数值!')
            if y > 0:
                break
        tt = Area4(y, 5, 10)
        t = tt.pay()

    elif (x < 0) or (x > 4):
        print('请输入正确地址!')
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值