为什么我的 While 循环退出?

Why does my While loop Exit?

我的评论说明了我的推理思路,但显然我有问题。我的代码直接跳转到 "Your total debt is..."

# Dictionary of Bills

expenses = {'mortgage':[], 'personal':[], 'power':[], 'insurance':[], 'water':[], 'food':[], 'savings':[], 'fuel':[], 'phone':[], 'internet':[], 'credit':[], 'emergencies':[]}
totalDebt = 0
switch = "A"

while switch == switch.isalpha(): # Condition is true, switch is a letter  
for each in expenses: # Iterates through each bill  
    debt = input("%s: "%each) # User input   
    if debt.isdigit(): # checks to make sure they only enter numbers  
        debt = int(debt) # Converts debt to its integer value  
        totalDebt = totalDebt + debt # adds debt to keep a running total.  


    else: # User entered something other than a number  
        print("Only enter digits!")  





print("Your total Debt is: $%i" %totalDebt)

input("Press Enter to continue: ")

print("What is this fortnights Income?")

你的条件在这里没有任何意义:

while switch == switch.isalpha(): # Condition is true, switch is a letter  

switch.isalpha() returns TrueFalseswitch 本身不会等于这两个值中的任何一个,因此整个表达式总是 False。删除相等性测试:

while switch.isalpha():  # Condition is true, switch is a letter  

请注意,您的代码实际上从未更改 switch,因此现在您的循环将永远继续下去。

虽然是假的

>>> switch
'A'
>>> switch.isalpha()
True
>>> switch == switch.isalpha()
False

您必须使用 switch.isalpha() 允许