You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python函数间参数传递值丢失问题(非全局变量方案)

问题分析与解决

你遇到的问题根源在于Python中浮点数属于不可变类型,传递参数时会创建原变量的副本。子函数里修改的只是这个副本,不会影响主函数中的原变量,所以返回主菜单后subtotal会被重置。

不用全局变量的解决方法很直接:让子函数返回修改后的subtotal,主函数调用子函数时接收返回值并更新自身的subtotal变量。

修改后的关键逻辑

1. 主菜单中接收子函数返回的新值

在main_menu函数里,调用子函数时必须接收返回的更新后的值,覆盖原有subtotal:

if choice == 1:
  # 接收appetizers返回的最新subtotal,替换原有值
  subtotal = appetizers(subtotal)

2. 子函数确保返回最新的subtotal

appetizers函数中,用户选择返回主菜单时已经有return subtotal的逻辑,这个要保留;每次修改subtotal后,循环会继续使用更新后的值,逻辑本身无需额外调整。

完整修改后的代码示例

def main_menu(subtotal):
  while True:
    print("1) Appetizers")
    print("2) Option 2")
    print("3) Option 3")
    print("4) Option 4")
    print("5) Option 5")
    print(" ")
    print("Current overall subtotal: $" + str(round(subtotal,2)))
 
    while True:
      try:
        choice = int(input("What is your choice? "))
        break
      except ValueError:
        print("Please enter whole numbers only.")


    while choice > 5 or choice < 1:
      print("Please choose an option from 1 to 5")
      try:
        choice = int(input("what is your choice? "))
      except ValueError:
        print("Please enter whole numbers only.")

    if choice == 1:
      subtotal = appetizers(subtotal)
    elif choice == 2:
      subtotal = option_2(subtotal)
    elif choice == 3:
      subtotal = option_3(subtotal)
    elif choice == 4:
      subtotal = option_4(subtotal)
    elif choice == 5:
      end(subtotal)
      return subtotal

def appetizers(subtotal):
  while True:
    print("1) Option 1")
    print("2) Option 2")
    print("3) Option 3")
    print("4) Return to Main Menu")
    print(" ")
    print("Current overall subtotal: $" + str(round(subtotal,2)))
   
    product_amount = 1

    while True:
      try:
        choice = int(input("What is your choice? "))
        break
      except ValueError:
        print("Please enter whole numbers only.")

    while choice > 4 or choice < 1:
      print("Please choose an option from 1 to 4")
      try:
        choice = int(input("what is your choice? "))
      except ValueError:
        print("Please enter whole numbers only.")

    if choice == 4:
      return subtotal
    else:
      while True:
        try:
          product_amount = int(input("How many would you like? "))
          break
        except ValueError:
          print("Please enter whole numbers only.")

    while product_amount > 100000 or product_amount < 1:
      print("Please choose an option from 1 to 100,000")
      product_amount = int(input("How many would you like? "))

    if choice == 1:
      subtotal = subtotal + (product_amount * 4.99)
    elif choice == 2:
      subtotal = subtotal + (product_amount * 2.99)
    elif choice == 3:
      subtotal = subtotal + (product_amount * 8.99)

# 其他子函数需遵循相同逻辑,示例如下
def option_2(subtotal):
  # 自定义处理逻辑
  subtotal += 5.99
  return subtotal

def end(subtotal):
  print(f"Final subtotal: ${round(subtotal,2)}")

核心逻辑说明

  • 所有修改subtotal的子函数,必须在结束时返回最新的subtotal值
  • 主函数调用子函数时,必须将返回值重新赋值给自身的subtotal变量,以此保持数值的持续更新

内容的提问来源于stack exchange,提问作者user20028948

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 22:01:08