求一段计算数字的python代码
求助目的:为了计算不同产品的运输费求助原因:我自己乱摸索凑的代码一直报错,实在没辙了
求助要求:1.该代码要能保存为py文件,以便cmd调用
2.【】黑括号内的内容不展示,只是说明想要实现的目的
求助内容:
请输入数字选择公式序号:1.苹果 2.香蕉 3.桃子
【如果我选择了1】
请输入重量:
请输入体积:
请输入件数:
【如果件数≥20,则用右边这个公式计算:重量/2*3+体积/4*5+6】
【如果件数<20,则用右边这个公式计算:重量/3*4+体积/5*6+7】
公式的运算结果是:
===========================================
请输入数字选择公式序号:1.苹果 2.香蕉 3.桃子
以上是一个计算片段,如果我选择1,就用1号产品的公式计算,如果我选择2号产品,就用2号产品的公式计算,每次计算都要判定件数,计算完成后接着让我再次选择。为了方便就用一个产品的公式计算3种产品吧
def calculate_fee(product, weight, volume, quantity):
if product == "apple":
if quantity >= 20:
return weight / 2 * 3 + volume / 4 * 5 + 6
else:
return weight / 3 * 4 + volume / 5 * 6 + 7
elif product == "banana":
if quantity >= 20:
return weight / 1.5 * 2 + volume / 3 * 4 + 8
else:
return weight / 2.5 * 3 + volume / 4 * 5 + 9
elif product == "peach":
if quantity >= 20:
return weight / 2 * 4 + volume / 3 * 6 + 10
else:
return weight / 3 * 5 + volume / 5 * 7 + 11
else:
print("Invalid product choice")
return None
def main():
while True:
print("请输入数字选择公式序号:1.苹果 2.香蕉 3.桃子")
choice = input("选择产品序号:")
if choice == '1':
product = "apple"
elif choice == '2':
product = "banana"
elif choice == '3':
product = "peach"
else:
print("无效选择,请重新输入。")
continue
try:
weight = float(input("请输入重量:"))
volume = float(input("请输入体积:"))
quantity = int(input("请输入件数:"))
except ValueError:
print("输入无效,请输入有效的数字。")
continue
fee = calculate_fee(product, weight, volume, quantity)
if fee is not None:
print(f"公式的运算结果是:{fee:.2f}")
print("==========================================")
if __name__ == "__main__":
main()
# transport_fee_calculator.py
def get_product_name(product_id):
products = {
1: "苹果",
2: "香蕉",
3: "桃子"
}
return products.get(product_id, "未知产品")
def calculate_fee(weight, volume, quantity):
if quantity >= 20:
fee = (weight / 2) * 3 + (volume / 4) * 5 + 6
else:
fee = (weight / 3) * 4 + (volume / 5) * 6 + 7
return fee
def main():
while True:
print("\n请输入数字选择产品序号:")
print("1. 苹果")
print("2. 香蕉")
print("3. 桃子")
print("4. 退出")
try:
choice = int(input("请输入选择(1-4):"))
except ValueError:
print("无效输入,请输入数字1-4。")
continue
if choice == 4:
print("退出程序。")
break
elif choice in :
product_name = get_product_name(choice)
print(f"您选择了:{product_name}")
try:
weight = float(input("请输入重量(kg):"))
volume = float(input("请输入体积(m³):"))
quantity = int(input("请输入件数:"))
except ValueError:
print("无效输入,请输入正确的数字格式。")
continue
fee = calculate_fee(weight, volume, quantity)
print("===========================================")
print(f"{product_name} 的运输费计算结果:{fee:.2f} 元")
print("===========================================")
else:
print("无效选择,请输入1-4之间的数字。")
if __name__ == "__main__":
main() 结果打印公式:
def calculate_fee(product, weight, volume, quantity):
if product == "apple":
if quantity >= 20:
formula = f"({weight} / 2 * 3) + ({volume} / 4 * 5) + 6"
result = weight / 2 * 3 + volume / 4 * 5 + 6
else:
formula = f"({weight} / 3 * 4) + ({volume} / 5 * 6) + 7"
result = weight / 3 * 4 + volume / 5 * 6 + 7
elif product == "banana":
if quantity >= 20:
formula = f"({weight} / 1.5 * 2) + ({volume} / 3 * 4) + 8"
result = weight / 1.5 * 2 + volume / 3 * 4 + 8
else:
formula = f"({weight} / 2.5 * 3) + ({volume} / 4 * 5) + 9"
result = weight / 2.5 * 3 + volume / 4 * 5 + 9
elif product == "peach":
if quantity >= 20:
formula = f"({weight} / 2 * 4) + ({volume} / 3 * 6) + 10"
result = weight / 2 * 4 + volume / 3 * 6 + 10
else:
formula = f"({weight} / 3 * 5) + ({volume} / 5 * 7) + 11"
result = weight / 3 * 5 + volume / 5 * 7 + 11
else:
print("Invalid product choice")
return None, None
return formula, result
def main():
while True:
print("请输入数字选择公式序号:1.苹果 2.香蕉 3.桃子")
choice = input("选择产品序号:")
if choice == '1':
product = "apple"
elif choice == '2':
product = "banana"
elif choice == '3':
product = "peach"
else:
print("无效选择,请重新输入。")
continue
try:
weight = float(input("请输入重量:"))
volume = float(input("请输入体积:"))
quantity = int(input("请输入件数:"))
except ValueError:
print("输入无效,请输入有效的数字。")
continue
formula, fee = calculate_fee(product, weight, volume, quantity)
if fee is not None:
print(f"公式:{formula} = {fee:.2f}")
print("==========================================")
if __name__ == "__main__":
main()
你的思路已经这么清晰了,可以直接让ai生成呀,能这么清晰的表述自己的需求已经是很好的提示词了。 def calculate_formula(weight, volume, count):
if count == 20:
result = (weight / 2 * 3) + (volume / 4 * 5) + 6
else:
result = (weight / 3 * 4) + (volume / 5 * 6) + 7
return result
def main():
while True:
print("请输入数字选择公式序号:1.苹果 2.香蕉 3.桃子")
choice = input()
if choice in ['1', '2', '3']:
if choice == '1':
print("【如果我选择了1】")
weight = float(input("请输入重量:"))
volume = float(input("请输入体积:"))
count = int(input("请输入件数:"))
result = calculate_formula(weight, volume, count)
print(f"公式的运算结果是:{result}")
print("==========================================")
elif choice == '2':
print("【如果我选择了2】")
# 可以在这里添加香蕉的逻辑
elif choice == '3':
print("【如果我选择了3】")
# 可以在这里添加桃子的逻辑
else:
print("输入有误,请重新输入!")
if __name__ == "__main__":
main()
ai直接生成,未修改,可以运行:
def calculate_transport_cost(weight, volume, quantity):
if quantity >= 20:
cost = (weight / 2 * 3) + (volume / 4 * 5) + 6
else:
cost = (weight / 3 * 4) + (volume / 5 * 6) + 7
return cost
def main():
while True:
print("请输入数字选择公式序号:1.苹果 2.香蕉 3.桃子")
product_choice = input("请选择产品(1-3): ")
if product_choice not in ['1', '2', '3']:
print("无效的选择,请重新输入!")
continue
weight = float(input("请输入重量: "))
volume = float(input("请输入体积: "))
quantity = int(input("请输入件数: "))
cost = calculate_transport_cost(weight, volume, quantity)
print(f"公式的运算结果是:{cost}")
print("==========================================")
continue_choice = input("是否继续计算?(y/n): ")
if continue_choice.lower() != 'y':
break
if __name__ == "__main__":
main()
可以试试文心一言这类的ai,直接生成,需要已经说的很清晰了
def calculate_apple(weight, volume, quantity):
if quantity >= 20:
return (weight / 2 * 3 + volume / 4 * 5 + 6)
else:
return (weight / 3 * 4 + volume / 5 * 6 + 7)
def calculate_banana(weight, volume, quantity):
# 假设香蕉的计算公式与苹果不同,这里需要您提供具体的公式
# 例如:
return weight * 2 + volume * 3 + quantity * 4
def calculate_peach(weight, volume, quantity):
# 假设桃子的计算公式与苹果不同,这里需要您提供具体的公式
# 例如:
return weight * 1.5 + volume * 2.5 + quantity * 3
def main():
while True:
print("请输入数字选择公式序号:1.苹果 2.香蕉 3.桃子")
choice = input()
if choice not in ['1', '2', '3']:
print("无效的选择,请重新输入。")
continue
print("请输入重量:")
weight = float(input())
print("请输入体积:")
volume = float(input())
print("请输入件数:")
quantity = int(input())
if choice == '1':
cost = calculate_apple(weight, volume, quantity)
elif choice == '2':
cost = calculate_banana(weight, volume, quantity)
elif choice == '3':
cost = calculate_peach(weight, volume, quantity)
print(f"公式的运算结果是:{cost}")
print("===========================================")
# 询问用户是否继续
continue_choice = input("是否继续计算?(y/n): ")
if continue_choice.lower() != 'y':
break
if __name__ == "__main__":
main() 学习,试试看
页:
[1]
2