Gcap
Gcap

Reputation: 378

python - breaking out of nested conditional to top of original loop

I'm having issues with this part of my code:

if(input not in status_list):
    print("Invalid Entry, try again.")
    break

The break exits the whole program, I just want to go back to the beginning of the program (to while(1):)
I've tried pass, continue, return can't think of anything else.. can anyone help?? Thanks :)
Also it's reading this variable income as a string still..: income = int(input("Enter taxable income: ")) The error message I get is "TypeError: 'str' object is not callable"

import subprocess
status_list = ["s","mj","ms","h"]


while(1):
    print ("\nCompute income tax: \n")
    print ("Status' are formatted as: ")
    print ("s = single \n mj = married and filing jointly \n ms = married and filing seperately \n h = head of household \n q = quit\n")
    input = input("Enter status: ")
    if(input == 'q'):
        print("Quitting program.")
        break
    if(input not in status_list):
        print("Invalid Entry, try again.")
        break 

    income = int(input("Enter taxable income: "))
    income.replace("$","")
    income.replace(",","")

    #passing input to perl files
    if(input == 's'):
        subprocess.call("single.pl")
    elif(input == 'mj'):
        subprocess.call("mj.pl", income)
    elif(input == 'ms'):
        subprocess.call("ms.pl", income)
    else:
        subprocess.call("head.pl", income)

Upvotes: 0

Views: 265

Answers (3)

Nathaniel Mallet
Nathaniel Mallet

Reputation: 401

Continue is working correctly. The problem with your script is that you're trying to call replace() on an int:

income = int(input("Enter taxable income: "))
# income is an int, not a string, so the following fails
income.replace("$","")

You can do something like this instead:

income = int(input("Enter taxable income: ").replace("$", "").replace(",", ""))

Upvotes: 0

David Marx
David Marx

Reputation: 8558

Your problem isn't continue, it's that you have an unresolved error further down in your code. Continue is doing exactly what it's supposed to (i.e. you want continue in that conditional).

You're renaming input as a string, so the name no longer points to the builtin input function in your code. This is why you don't use reserved keywords as variable names. Call your variable something other than "input" and your code should work fine.

Upvotes: 0

DSM
DSM

Reputation: 352979

input = input("Enter status: ")

You rebind the name input from the input function to its result, which is a string. So the next time you call it, after continue does its work, input doesn't name a function any more, it's just a string, and you can't call a string, hence

TypeError: 'str' object is not callable 

Use continue, and change your variable name so as not to clobber the function.

Upvotes: 2

Related Questions