修复 "global" 程序问题?

Fixing "global" issue with program?

最初我在存款和取款函数中有 "global" 语句。我的教授告诉我,我不能在这项作业中使用全局语句。顶部有一个由 Codio 创建的变量,我无法移动它。 我必须在我的函数中访问该变量,我确实这样做了。现在我需要以 "account_balance" 变量在每次取款和存款后更新的方式将我的功能设置为 return。目前代码功能基本没问题,但在每次对钱进行操作后,我输入 "b" 作为余额功能,之前的操作都被清除了,我又回到了默认余额。我在这里做错了什么?代码如下:

    import sys

#account balance 
account_balance = float(500.25)


#<--------functions go here-------------------->
#printbalance function
def balance():
  print('Your current balance:\n%.2f' % account_balance)


#deposit function
def deposit(temp):
  deposit_amount = float(input('How much would you like to deposit today?\n'))
  temp = temp + deposit_amount
  print('Deposit was $%.2f, current balance is  $%.2f' % (deposit_amount, temp))
  return temp

#withdraw function
def withdraw(temp2):

  withdrawal_amount = float(input('How much would you like to withdraw today?\n'))
  if withdrawal_amount > temp2:
    print('$%.2f is greater than your account balance of $%.2f ' % (withdrawal_amount, temp2))
  else:
    temp2 -= withdrawal_amount
    account_balance = temp2
    print('Withdrawal amount was $%.2f, current balance is $%.2f' % (withdrawal_amount, temp2))
    return temp2

#User Input goes here, use if/else conditional statement to call function based on user input

userchoice = ''

while userchoice!= 'Q':
  userchoice = input('What would you like to do?\n(B)alance?, (W)ithdraw?, (D)eposit?, (Q)uit?\n')
  userchoice = userchoice.upper()

  if (userchoice == 'B'):
    balance ()
  elif userchoice == 'W':
    withdraw (account_balance)
  elif userchoice == 'D':
    deposit (account_balance)
  elif userchoice == 'Q':
    print('Thank you for banking with us.')
    break

问题是您传递给函数的值是一个副本,而不是对原始值的引用。你可以做以下两件事之一:

  1. 在函数中使用全局语句
  2. 将函数中的 return 值存储到全局变量中

第一个选项

我想这就是你的教授所说的不允许:

def deposit():
  global account_balance
  deposit_amount = float(input('How much would you like to deposit today?\n'))
  account_balance = account_balance + deposit_amount
  print('Deposit was $%.2f, current balance is  $%.2f' % (deposit_amount, temp))
  return account_balance

第二个选项

while userchoice!= 'Q':
  userchoice = input('What would you like to do?\n(B)alance?, (W)ithdraw?, (D)eposit?, (Q)uit?\n')
  userchoice = userchoice.upper()

  if (userchoice == 'B'):
    balance ()
  elif userchoice == 'W':
    account_balance = withdraw (account_balance)
  elif userchoice == 'D':
    account_balance = deposit (account_balance)
  elif userchoice == 'Q':
    print('Thank you for banking with us.')
    break

由于不允许使用全局变量,所以需要使用存取函数返回的变量。

while userchoice!= 'Q':
userchoice = input('What would you like to do?\n(B)alance?, (W)ithdraw?, (D)eposit?, (Q)uit?\n')
userchoice = userchoice.upper()

if (userchoice == 'B'):
  balance ()
elif userchoice == 'W':
    new_balance = withdraw(account_balance)
    if new_balance:
        account_balance = new_balance
elif userchoice == 'D':
    account_balance = deposit (account_balance)
elif userchoice == 'Q':
    print('Thank you for banking with us.')
    break

取款功能 returns 新账户余额,或者打印出您账户中的资金不足。在调用 withdraw function() 之后,您检查它是否返回了任何东西。如果是这样,您使用存储在 new_balance.

中的返回值更新 account_balance

对于存款函数,您只需使用函数返回的值更新 account_balance。