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.3k3 gold badges61 silver badges67 bronze badges
asked Jul 2, 2015 at 13:41
0

2 Answers 2

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.

answered Jul 2, 2015 at 13:44
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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: 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.
@Boogz: in this context, you probably want to read Asking the user for input until they give a valid response
0

The While is False

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

You must use switch.isalpha() allown

answered Jul 2, 2015 at 13:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.