0

My comments illustrate my line of reasoning but clearly I've got something wrong. My code just jumps straight to "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?")
Sandy Chapman
  • 11,133
  • 3
  • 58
  • 67
Boogz
  • 9
  • 4

2 Answers2

11

Your condition doesn't make any sense here:

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

switch.isalpha() returns either True or False. switch itself will not be equal to either of those two values, so the whole expression is always going to be False. Remove the equality test:

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

Note that your code never actually changes switch, so now your loop is going to continue forever.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • It's so obvious now! I knew switch won't change (yet) it was driving me up the wall that it wouldn't work so I hadn't added the change switch clause. – Boogz Jul 02 '15 at 13:48
  • Why does ending the while loop end the for loop that it is nested within?for each in expenses: # Itterates through each bill while switch.isalpha(): # Condition is true, switch is a letter 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. switch = "0" # Condition is False, switch is a number – Boogz Jul 02 '15 at 14:07
  • @Boogz: You need to set `switch` back to a letter in the *next iteration*. `switch` doesn't magically change bak to `'A'` when `for` loops to the next expense. – Martijn Pieters Jul 02 '15 at 14:09
  • @Boogz: in this context, you probably want to read [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658) – Martijn Pieters Jul 02 '15 at 14:13
0

The While is False

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

You must use switch.isalpha() allown

Tuxlife
  • 519
  • 5
  • 6