如何在输出中移除括号和逗号

2024-03-28 23:49:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python为一个使用数组和函数的银行应用程序编写一个程序。这是我的密码:

NamesArray=[]
AccountNumbersArray=[]
BalanceArray=[]
def PopulateAccounts():
    for position in range(5):
        name = input("Please enter a name: ")
        account = input("Please enter an account number: ")
        balance = input("Please enter a balance: ")
        NamesArray.append(name)
        AccountNumbersArray.append(account)
        BalanceArray.append(balance)
def SearchAccounts():
    accounttosearch = input("Please enter the account number to search: ")
    for position in range(5):
        if (accounttosearch==AccountNumbersArray[position]):
            print("Name is: " +NamesArray[position])
            print(NamesArray[position],"account has the balance of: ",(BalanceArray[position]))
            break
    if position>4:
        print("The account number not found!")


while True:
    print("**** MENU OPTIONS ****")
    print("Type P to populate accounts")
    print("Type S to search for account")
    print("Type E to exit")
    choice = input("Please enter your choice: ")
    if (choice=="P"):
        PopulateAccounts()
    elif (choice=="S"):
        SearchAccounts()
    elif (choice=="E"):
        print("Thank you for using the program.")
        print("Bye")
        break
    else:
        print("Invalid choice. Please try again!")

现在一切正常,但当我运行程序并搜索帐户时。例如,当我搜索一个帐户时,输出显示的是('name', 'account has the balance of: ', 312),而不是name account has the balance of: 312,我该如何解决这个问题?你知道吗


Tags: thetonameforinputpositionaccountprint
2条回答

排队

print(NamesArray[position],"account has the balance of: ",(BalanceArray[position]))

应该使用字符串串联来添加不同的字符串。一种方法是:

print(NamesArray[position] + " account has the balance of: " + str(BalanceArray[position])) 

你用的是老版的python, 将您的线路替换为:

print(NamesArray[position] + "account has the balance of: " + (BalanceArray[position]))

用“+”代替逗号

相关问题 更多 >