学习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('请输入正确地址!')