【发布时间】: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