python - 我的两个函数不能正常工作,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!")
解决方案
你有逻辑错误。
只是改变
money + (balance[foundtheposition])
至
balance[foundtheposition] = balance[foundtheposition] + money
或使用速记运算符,例如
balance[foundtheposition] += money
相同的
withdraw - (balance[thenumber])
干杯...
推荐阅读
- php - 使用 Laravel Storage 获取文件的完整路径
- javascript - 如何在选择框中添加禁用选项或默认选项?
- wpf - 使用 click、MouseEnter 和 MouseLeave 的按钮/图像切换资源
- java - 从 IDE 运行 Spark SQL 应用程序的 setJars() 方法
- r - Parellize xgboost 建模模拟
- c# - 使用 Entity Framework Core 的 SQL Server 查询中的参数化 OPENJSON
- python - 获取动作计数
- spinnaker - 如何删除附加到集群的 Kubernetes 提供者帐户。(该集群已删除)
- c++ - 该函数应返回 1 但它返回 0
- php - cakephp 3 get 方法不返回字段