首页 > 解决方案 > 我的两个函数不能正常工作,IDE 不显示错误或崩溃

问题描述

存款功能和提款功能不起作用。填充帐户后,我可以选择 D 或 W 菜单选项并输入任何数字,而不会导致程序崩溃或导致错误。该程序似乎工作正常,但是当您使用 S 选项检查余额时,它们没有更新。

Names=[]
accountnumbers=[]
balance=[]


def populatelist():
 position=0
 while(position<=2):
   yourname= str(input("Please enter a name: "))
   Names.append(yourname)
   account = int(  input("Please enter an account number: " ))
   accountnumbers.append(account)
   totalbalance = int(  input("Please enter a balance: "))
   balance.append(totalbalance)
   position = position + 1




##################################### DEPOSIT FUCNTION 
def deposit(accountnumber):
   foundposition=-1
   position=0
   if (len(accountnumbers)>0):
      while (position <=2):
         if (accountnumber==accountnumbers[position]):
            return position
         position = position + 1
   return foundposition




####################################  WITHDRAW FUNCTION
def withdraw(accountnumber):
   foundposition=-1
   position=0
   if (len(accountnumbers)>0):
      while (position <=2):
         if (accountnumber==accountnumbers[position]):
            return position
         position = position + 1
   return foundposition



def findingaccount(accountnumber):
   foundposition=-1
   position=0
   if (len(accountnumbers)>0):
      while (position <=2):
         if (accountnumber==accountnumbers[position]):
            return position
         position = position + 1
   return foundposition



def menuoptions():
   print ("**** MENU OPTIONS ****")
   print ("Type P to populate accounts")
   print ( "Type S to search for account")
   print ("Type E to exit")
   print ("Type D to deposit Amount")
   print ("Type W to withdraw Amount")
   choice = str(input("Please enter your choice: "))
   return choice



response=""
while response!= "E":
 response = menuoptions()
 if response=="P":
   populatelist()



 ########################### Deposit OPTION
 elif response=="D":
  searchaccount = int(input("Please enter the account number to add deposit: "))
  foundtheposition = deposit(searchaccount)
  money = int(input("Please enter the amount to be deposited: "))
  money + (balance[foundtheposition])



 ###########################  WITHDRAW OPTION
 elif  response=="W":
  searchaccount = int(input("Please enter the account number to withdraw: "))
  thenumber = withdraw(searchaccount)
  withdraw = int(input("how much for withdraw"))
  withdraw - (balance[thenumber])
  if (balance[thenumber]) < withdraw :
   print("ERROR: Not enough balance")




 elif response=="S":
  searchaccount = int(input("Please enter the account number to search: "))
  foundaposition = findingaccount(searchaccount)
  if ( foundaposition == -1 ):
   print ("The account number not found!")
  else:
   print ("Name is:" + str( Names[foundaposition])) 
   print (str(Names[foundaposition]) + " " + "account has the balance of :" +str(balance[foundaposition]))


 elif response=="E":
  print ("Thank you for using the program.")
  print ("Bye")
  exit

 else:
  print ("Invalid choice. Please try again!")

标签: pythonpython-3.x

解决方案


你有逻辑错误。

只是改变

money + (balance[foundtheposition])

balance[foundtheposition] = balance[foundtheposition] + money

或使用速记运算符,例如

balance[foundtheposition] += money

相同的

withdraw - (balance[thenumber])

干杯...


推荐阅读