【问题标题】:Stuck on an assignment. At least part of it. ATM Banking choices卡在一个任务上。至少有一部分。 ATM 银行业务选择
【发布时间】:2018-05-13 18:02:30
【问题描述】:

以下是我为执行此银行 atm 任务而编写的代码。我似乎无法在代码的余额中加减。我很确定我做错了什么。以下是任务所涉及的部分。在选择选项 P、S 和 E 方面,其他一切正常。选项 D 和 W 是我遇到问题的地方。任何帮助都会很棒。谢谢!

如果用户键入 D 则: · 向用户询问帐号。 · 在 accountnumbers() 数组中搜索该帐号并找到它的位置。 · 向用户询问要存入的金额。 · 将存款金额添加到该帐户的余额中。 · 如果用户键入 W 则: · 向用户询问帐号。 · 在 accountnumbers() 数组中搜索该帐号并找到它的位置。 · 向用户询问要提取的金额。 · 从该账户的余额中减去提款金额。

namelist=[]
accountnumberslist=[]
balancelist=[]

def populatelist():
    print ('Great! Please enter the information below')
    namecount=0
    #This loops collects the five names,accounts,balances, and appends them to the namelist
    while(namecount< 2):
        name= input('Enter a name: ')
        namelist.append(name)
        accountnumber = input('Please enter your Account Number: ')
        accountnumberslist.append(accountnumber)
        balances = input('Please enter your Balance: ')
        balancelist.append(balances)
        namecount = namecount + 1
    return

def displayall():
    print ('I am inside display function')
    position =0
    #This loop, prints one name at a time from the zero position to the end. 
    while ( position < 2):
        displayone(position)
        position = position + 1
    return

def displayone(position):
    print ('Account Holder:',namelist[position])
    print ('Balance:',balancelist[position])
    return

def calculatedeposit():
      position = 0
      while (position < 2):
        depamount = int(input('Please enter the amount to deposit'))
        balancelist[position] = balancelist[position] + depamount
        position = position + 1
      return      

def calculatewithdrawal()
       position = 0
       while (position < 2):
        withmount = int(input('Please enter the amount to deposit'))
        balancelist[position] = balancelist[position] + withamount
        position = position + 1

#What does it receive. Account Number to search.
#what does it do? Searches for the Account Holder.
#what does it send back. Position of the Account Holder, if not found, -1
def searchforacct(accounttosearch):
    foundposition=-1  # assume that it is not going to be found. 
    position=0
    while (position < 2):
        if (accounttosearch==accountnumberslist[position]):
            foundposition = position
            break
        position = position + 1
    return foundposition

#This function will display the menu, collect the users response and send back
def displaymenu():
    print ('Enter P to Populate Data')
    print ('Enter S to Search for Account ')
    print ('Enter D to Deposit Amount ')
    print ('Enter W to Withdraw Amount ')
    print ('Enter E to Exit')
    choice = input('How can we help you today?:')
    return choice

print("=====================================")
print(" Welcome to Liberty City Bank ATM    ")
print("=====================================")
#main
response=''
while response!= 'E' and response!='e':
    response = displaymenu()
    if response=='P' or response=='p':
        populatelist()
    elif response=='S' or response=='s':
        accounttosearch = input('Please enter the Account Number to search:')
        foundpos = searchforacct(accounttosearch)
        if ( foundpos == -1 ):
            print ('Account not found')
        else:
            displayone(foundpos)
    elif response=='D' or response=='d':
         accounttosearch = input('Please enter the Account Number for Deposit:')
         foundpos = searchforacct(accounttosearch)
         if ( foundpos == -1 ):
            print ('Account not found')
         else:
          calculatedeposit()

    elif response=='W' or response=='w':
          accounttosearch = input('Please enter the Account Number for Withdrawal:')
          foundpos = searchforacct(accounttosearch)
          if ( foundpos == -1 ):
            print ('Account not found')
          else:
            calculatewithdrawal()

    elif response=='E' or response=='e':
        print ('Thank you for choosing Liberty City Bank!')
        print ('Have a Nice Day!')
    else:
        print ('Invalid choice. Please try again')

【问题讨论】:

  • 您能否更详细地解释一下您的代码无法按预期工作的原因?
  • 当调用foundpos = searchforacct(accounttosearch) 时(顺便说一句,在camelCase 中输入这些会使这更具可读性)你设置了位置。但是当您调用calculatewithdrawal() 或calculatedeposit() 时,您会重置位置。您需要将该位置作为 argument 传递给这两个函数,并删除 position = 0 行
  • 您真的根本不需要在这些循环中使用while...您一次只更新一个值。
  • 最后一句话,balancelist 是 strings 的列表...您从不将它们转换为整数,因此您要添加/减去整数和字符串。 HTH
  • 感谢大家的意见。 Mangohero1,关于“while's”,你是对的。

标签: python python-3.x


【解决方案1】:

您的代码无法编译,在第 45 行添加 : 在您的 def 之后。在第 45 行更改 withamount to withmount。然后在第 45 行更改

balancelist[position] = balancelist[position] + withamount

到这里

 balancelist[position] = int(balancelist[position]) - withamount

您需要将余额列表中的字符串转换为 int。您还需要从列表中减去 (-) 而不是添加 (+) 作为取款。

您的代码中还有一些其他错误,但这足以让您启动并运行。

【讨论】:

    【解决方案2】:

    您无需查看从用户输入中找到的“帐户”,而是遍历前 2 个余额列表并添加用户给定值。

    【讨论】:

      猜你喜欢
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      • 2022-01-13
      • 2013-06-13
      • 1970-01-01
      相关资源
      最近更新 更多