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.3k3 gold badges61 silver badges67 bronze badges
2 Answers 2
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.
answered Jul 2, 2015 at 13:44
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Boogz
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
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
Martijn Pieters
@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
@Boogz: in this context, you probably want to read Asking the user for input until they give a valid response
The While is False
>>> switch
'A'
>>> switch.isalpha()
True
>>> switch == switch.isalpha()
False
You must use switch.isalpha() allown
Comments
lang-py