我的while循环跳过if语句,直接进入else语句

时间:2019-06-03 18:15:19

标签: python while-loop scope

我有一个while循环,似乎正在跳过我的第一个if语句,而直接移到else语句。我已经三遍检查了代码,这似乎是正确的,但它仍然不会确认我的第一个if语句。 (程序中有很多代码未包含在此问题中)

我尝试将else语句移到不同的索引,并重新配置initialize_objects()函数以返回主列表,而不是None。似乎没有任何作用,因为我没有收到错误,无论如何它只会在底部运行else语句。

def initialize_objects():
        global jackson_checking, jackson_savings, jackson_business, master_list



        jackson_checking = CheckingAccount('Jackson', 4100)
        jackson_savings = SavingsAccount('Jackson', 100000)
        jackson_business = BusinessAccount('Jackson', 15000)


        master_list = [[jackson_checking, 1, 1], [jackson_savings, 1, 2], [jackson_business, 1, 3]]


        return None

    initialize_objects()

    while isSessionOn is True:
        print('Welcome to 24-hour ATM service.')
        print('insert your card.')


        # Card reading the customer info representation
        customerID = input('Enter your customer id number: ')
        print("\n")


        cust_accounts = []
        for i in master_list:
            if i[1] == customerID:
                cust_accounts.append(i[2])
                isCustomer = True 

        if isCustomer is True:
            isAccountSelected = False


        else:
            print("cannot find your record.")
            print("Please get your card.")
            print("Closing this session...")

我期望发生的事情是if customerID == 1,然后该程序应转到isAccountSelected(此问题中不包括)下面的另一个while循环。相反,当我输入1作为customerID的输入时,发生的事情是,它直接移到底部的else语句,并继续询问另一个CustomerID ...

它看起来像这样:

Welcome to 24-hour ATM service.
insert your card.
Enter your customer id number: 1


cannot find your record.
Please get your card.
Closing this session...
Welcome to 24-hour ATM service.
insert your card.
Enter your customer id number: 1


cannot find your record.
Please get your card.
Closing this session...
Welcome to 24-hour ATM service.
insert your card.
Enter your customer id number:

1 个答案:

答案 0 :(得分:2)

这是因为Python中的input总是返回一个字符串。因此,当用户输入1作为其ID时,Python程序实际上使customerID为字符串"1"。因此,当您尝试比较1=="1"时,它总是失败并且总是进入else块。