写了一段代码,请师兄师姐帮看下,给些建议或更简单的方法

提示用户输入:1.重量; 2.地点编号
快递费算法:
首重:3公斤
3公斤以内: 东三省/宁夏/青海/海南 12元 新疆/西藏:20元 港澳台/国外:不接受寄件 其他:10元
超过3公斤部分:东三省/宁夏/青海/海南 每公斤10元 新疆/西藏:每公斤20元 港澳台/国外:联系总公司 其他:每公斤5元
用户输入重量和地点编号后显示快递费金额。
代码如下:
nqh=1001
name1=”东三省/宁夏/青海/海南”

xinxi=1002
name2=”新疆/西藏”

gangaotai=0000
name3=”港澳台/国外”

athon=1111
name4=”非偏远地区”

num=int(input(“请输入地点编号:”))

if num==nqh:
weight=float(input(“请输入快递重量(公斤):”))
if weight<=3:
print(“发往:”,name1,”,运费:”,weight12,”元!”)
else:
print(“发往:”,name1,”,运费:”,3
12+(weight-3)*10,”元!”)

elif num==xinxi:
weight=float(input(“请输入快递重量(公斤):”))
if weight<=3:
print(“发往:”,name2,”,运费:”,weight20,”元!”)
else:
print(“发往:”,name2,”,运费:”,3
20+(weight-3)*20,”元!”)

elif num==athon:
weight=float(input(“请输入快递重量(公斤):”))
if weight<=3:
print(“发往:”,name4,”,运费:”,weight10,”元!”)
else:
print(“发往:”,name4,”,运费:”,3
10+(weight-3)*5,”元!”)
else:
print(“不接受寄件!”)
不知是否规范或有没有更简单的方式?

讨论数量: 2

建议:

  1. 可以将地点编号和地点名称放在一个字典中,避免使用多个变量;
  2. 在输入重量时可以加上输入提示,更加友好;
  3. 可以将重量和运费计算部分提取出来,写成一个函数,避免代码重复。

改进后的代码如下:

locations = {1001: '东三省 / 宁夏 / 青海 / 海南', 1002: '新疆 / 西藏', 1111: '非偏远地区'}

def calculate_fee(weight, location): if location == 1001: if weight <= 3: return weight * 12 else: return 36 + (weight - 3) * 10 elif location == 1002: if weight <= 3: return weight * 20 else: return 60 + (weight - 3) * 20 elif location == 1111: if weight <= 3: return weight * 10 else: return 30 + (weight - 3) * 5 else: return None

location = int(input("请输入地点编号: ")) if location in locations: weight = float(input("请输入快递重量(公斤): ")) fee = calculate_fee(weight, location) if fee is not None: print("发往:", locations[location], ",运费:", fee, "元!") else: print("不接受寄件!") else: print("不接受寄件!")

来自ChatGPT

1年前 评论
Jason990420
import PySimpleGUI as sg

price = {
    "东三省"         : [12.0, 10.0],
    "宁夏"           : [12.0, 10.0],
    "青海"           : [12.0, 10.0],
    "海南"           : [12.0, 10.0],
    "新疆"           : [20.0, 20.0],
    "西藏"           : [20.0, 20.0],
    "港澳台/国外"    : ["不接受寄件", "联系总公司"],
    "其他非偏远地区" : [10.0,  5.0],
}

sg.theme('DarkBlue')
sg.set_options(font=("Courier New", 20))

places = list(price.keys())
width = max(map(len, places))*2
layout = [
    [sg.Text("发往地点"),
     sg.Listbox(places, size=(width, 8), enable_events=True, key='Place'),
     sg.Text("快递重量(公斤)"),
     sg.Input(size=width, enable_events=True, key="Weight")],
    [sg.StatusBar("", size=10, expand_x=True, key="Status")],
]
window = sg.Window('快递费用', layout)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    print(event, values)
    if event in ("Place", "Weight") and values["Place"]:
        place = values["Place"][0]
        try:
            weight = float(values["Weight"])
            if weight <= 0:
                weight = 0
        except ValueError:
            weight = 0
        if place in places:
            if weight == 0:
                window['Status'].update("快递重量错误")
                continue
            else:
                if weight <= 3:
                    cost = price[place][0]
                    if isinstance(cost, str):
                        window['Status'].update(price[place][0])
                    else:
                        window['Status'].update(f"发往:{place}, 运费:{cost}元!")
                else:
                    cost1, cost2 = price[place][0], price[place][1]
                    if isinstance(cost2, str):
                        window['Status'].update(cost2)
                    else:
                        window['Status'].update(f"发往:{place}, 运费:{cost1+(weight-3)*cost2}元!")

window.close()

file

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!