【发布时间】:2019-10-19 07:33:18
【问题描述】:
我有一个 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:
【问题讨论】:
-
按照习惯,你不应该使用
is True,而是使用== True。您只想在特定情况下使用is。 -
python 输入返回字符串,但是你想和整数比较!
标签: python while-loop scope