簡體   English   中英

我的兩個功能無法正常工作,IDE 不顯示錯誤或崩潰

[英]Two of my functions don't work correctly, IDE doesn't show errors or crashes

存款 function 和取款 function 不起作用。 填充帳戶后,我可以 select 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!")

你有邏輯錯誤。

只是改變

money + (balance[foundtheposition])

balance[foundtheposition] = balance[foundtheposition] + money

或使用速記運算符,例如

balance[foundtheposition] += money

同樣適用

withdraw - (balance[thenumber])

干杯...

您的存款場景需要將金額添加到所選賬戶中:

balance[thenumber] += money

公平警告除了帳戶未更新外,您的程序中還有其他錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM