【问题标题】:How to find the index of a value in an array and use it to display a value in another array with the same index如何在数组中找到一个值的索引并使用它来显示另一个具有相同索引的数组中的值
【发布时间】:2020-12-02 04:51:20
【问题描述】:

我是 Python 新手,一直在尝试“简单的银行程序”。

除了这一点,我什么都做对了:

如果用户键入 S 则:
向用户询问帐号。
在数组中搜索该帐号并找到它在 accountnumbers 数组中的位置。
在上述搜索中找到的位置显示名称和余额。

最初它应该只是通过帐户 1-5,但现在我无法想出一种方法来搜索帐号,如果它们是任何数字,而不仅仅是 1-5。例如

用户将他的帐号设为 34、445、340、2354 和 3245。完全随机的帐号,没有顺序。

这是我目前所拥有的

names = []
accountNumbers = []
balance = []

def displaymenu():
    print("**** MENU OPTIONS ****")
    print("Type P to populate accounts")
    print("Type S to search for account")
    print("Type E to exit")
    choiceInput()

def choiceInput():   
    choice = str(input("Please enter your choice: "))
    if (choice == "P"):
        populateAccount()
    
    elif (choice == "S"):
        accountNumb = int(input("Please enter the account number to search: "))
        if (accountNumb > 0) and (accountNumb < 6):
            print("Name is: " + str(names[accountNumb - 1]))
            print(names[accountNumb - 1] + " account has the balance of : $" + str(balance[accountNumb -1]))
        elif (accountNumb == accountNumbers):
            index = names.index(accountNumb)
            accountNumb = index
            print(names[accountNumb - 1] + " account has the balance of : $" + str(balance[accountNumb -1]))
        else:
            print("The account number not found!")
    
    elif (choice == "E"):
        print("Thank you for using the program.")
        print("Bye")
        raise SystemExit
    
    else:
        print("Invalid choice. Please try again!")
    displaymenu()

def populateAccount ():
        name = 0
        for name in range(5):
                Names = str(input("Please enter a name: "))
                names.append(Names)
                account ()
                name = name + 1
        
        
def account ():
    accountNumber = int(input("Please enter an account number: "))
    accountNumbers.append(accountNumbers)
    balances()
    
def balances ():
    balances = int(input("Please enter a balance: "))
    balance.append(balances)


displaymenu()

我曾尝试使用索引,但未能找到解决方案。

【问题讨论】:

    标签: arrays python-3.x indexing


    【解决方案1】:

    替换下面这行代码
    if (accountNumb &gt; 0) and (accountNumb &lt; 6):

    if (accountNumb &gt; 0) and (accountNumb &lt; len(accountNumbers)):

    【讨论】:

      【解决方案2】:

      我的错。我在添加帐号时搞砸了:

      def account ():
          accountNumber = int(input("Please enter an account number: "))
          accountNumbers.append(accountNumbers)
          balances()
      

      我添加了

      帐号

      不是

      账号

      代码应该是

      def account ():
          accountNumber = int(input("Please enter an account number: "))
          accountNumbers.append(accountNumber)
          balances()
      

      我做的searchArray函数也是:

      def searchArray(accountNumbers):
          x = int(input("Please enter an account number to search: "))
          y = accountNumbers.index(x)
          print("Name is: " + str(names[y]))
          print(str(names[y]) + " account has a balance of: " + str(balance[y]))
      

      菜鸟错误,不应该使用如此相似的对象名称。

      【讨论】:

        猜你喜欢
        • 2020-12-18
        • 1970-01-01
        • 2021-04-12
        • 2016-10-22
        • 1970-01-01
        • 2015-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多