与字典值比较(?)

时间:2019-06-05 06:28:09

标签: python-3.x

我目前正在做一个自我项目,一个简单的ATM。我使用的字典具有设定的名称,密码和用于储蓄和支票帐户的金额。当我尝试比较字典和用户输入中的值时,由于某种原因,它不起作用。

bank = [
    {'UserName': 'Bob', 'PIN': '0001', 'CheckingBalance': '1234', 'SavingsBalance': '12345'},
    {'UserName': 'Steve', 'PIN': '0002', 'CheckingBalance': '12', 'SavingsBalance': '123'},]

def withdraw():
    while "w":
        for record in bank:
            # checks for checking or savings account & amount to withdraw
            account = input("CHECKING (C) OR SAVINGS (S): ").lower
            amount = int(input("AMOUNT FOR WITHDRAWAL: "))
            # checks for enough balance and negatives for checking account
            if account == "c":
                if amount > record['CheckingBalance']:
                    amount = input("INVALID. PLEASE CHOOSE A DIFFERENT AMOUNT: ")
                elif amount <= 0:
                    amount = input("INVALID. PLEASE CHOOSE A DIFFERENT AMOUNT: ")
                else:
                    print("WITHDRAWAL SUCCESSFULLY COMPLETED.")
                    break

#this is part of the withdraw method and for some reason when I'm comparing the amount and record['CheckingBalance'] or record['SavingsBalance'], it doesn't work. 

def deposit():
    while "d":
        # checks for checking or savings account & amount to deposit
        account = input("CHECKING (C) OR SAVINGS (S): ").lower
        amount = int(input("AMOUNT TO DEPOSIT: "))
        # checks for 0 and negatives for checking account
        if account == "c":
            if amount <= 0:
                print(input("INVALID. PLEASE CHOOSE A DIFFERENT AMOUNT: "))
            else:
                print("DEPOSIT SUCCESSFULLY COMPLETED")
            break
        # checks for 0 and negatives for savings account
        if account == "s":
            if amount <= 0:
                print(input("INVALID. PLEASE CHOOSE A DIFFERENT AMOUNT: "))
            else:
                print("DEPOSIT SUCCESSFULLY COMPLETED")
                break

#this is the deposit method

#both of these methods, I'm having a bug where after I input an amount, it prompts me to "CHECKINGS (C) OR SAVINGS(S) for some reason.

1 个答案:

答案 0 :(得分:0)

您的字典包含字符串而不是整数。尝试更改

bank = [
    {'UserName': 'Bob', 'PIN': '0001', 'CheckingBalance': '1234', 'SavingsBalance': '12345'},
    {'UserName': 'Steve', 'PIN': '0002', 'CheckingBalance': '12', 'SavingsBalance': '123'},]

bank = [
    {'UserName': 'Bob', 'PIN': '0001', 'CheckingBalance': 1234, 'SavingsBalance': 12345},
    {'UserName': 'Steve', 'PIN': '0002', 'CheckingBalance': 12, 'SavingsBalance': 123},]

此外,您使用while "w":while "d":创建了while True循环(无限循环),但看不到这些循环有任何中断。