为什么我的While循环退出?

2024-10-03 02:40:28 发布

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

我的评论说明了我的推理思路,但很明显我错了。我的代码直接跳到“你的债务总额是…”

# 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?")

Tags: to代码inputis评论totaleachprint
2条回答

这是假的

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

你必须使用交换机.isalpha()全部

你的情况在这里毫无意义:

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

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

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

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

相关问题 更多 >