0

This is more a plea for help from the ubuntu community. I've done a lot of searches and reading and I guess I'm missing something. I'm trying to write a very basic program in python, here's the issue: I ask the question raw_input("question" ), then assign an if statement like so: This is the error:

The debugged program raised the exception unhandled NameError
"name 'tx_rate' is not defined"
File: /home/Budget.py, Line: 47.

This is the code:

ans1 = raw_input("Do you know your tax rate?" )
if 'ans1' == 'yes':
     tx_rate = raw_input("What is it?")
     float(tx_rate)
     tx_rate = float(tx_rate)
     print "Thank you! Will use tax rate %s, instead of 0.15!" % (tx_rate)
elif "ans1" == "no":
    print "Okay, that's alright, we'll use 0.15 as the average tax rate!"
    tx_rate = 0.15
else:
    print "Sorry, incorrect value, please answer yes or no."
gross_pay = (hrs * rate) * 4.0
net_pay = gross_pay - (gross_pay * tx_rate) * 4.0 [That last line is line 47]

The error comes from the variable for tx_rate never being assigned because weather I asnwer yes or no it runs the ELSE option

so what happens is that when I run the code, I get through a lot of questions, were I ask about earnings, and then when it loads that question up there, I answer yes or no and it prints the else option and then tells me error "name 'variable' is not defined] seemingly because, since it won't allow me to answer with yes or no it just tries to run code with a non existent variable. Could anyone help me fix this? Sorry if this isn't where I should ask!

  • 4
    You probably should ask this on Stack Overflow. With more details: The error will come with a line number, what is it? Can you reproduce your error with a short example? – muru Jul 26 '15 at 7:03
  • This is the error: The debugged program raised the exception unhandled NameError "name 'tx_rate' is not defined" File: /home/Budget.py, Line: 47 – Jerry White Jul 26 '15 at 7:16
  • Modified post above – Jerry White Jul 26 '15 at 7:21
  • sorry that didnt format properly – Jerry White Jul 26 '15 at 7:22
  • 1
    @muru is right, but just to help you out: 'ans1' == 'yes' and 'ans1' == 'no' compares two strings which are never equal :) Should be: if ans1 == 'yes' (no quotes). – Jacob Vlijm Jul 26 '15 at 7:49
2

You should probably try defining tx_rate=0.15 before your if...else conditions. Because when your user inputs nothing for the first question where you ask for yes or no it directly goes to the else part where there's no tx_rate so giving you error "variable not defined"

0

@jaysheel utekar is right. The problem is that, if your code ends up in the else case of your if statement, variable tx_rate is not defined. This causes the calculation in line 47 to throw an exception.

But there are a few more problems with your code: if 'ans1' == 'yes': will always result in False, because you are comparing two different strings and not a variable and a string. The correct code is: if ans1 == 'yes':

The same problem with: elif "ans1" == "no":. The correct code is: elif ans1 == "no":

You can rewrite your if statement in a much shorter form as follows:

if ans1 == 'yes':
    tx_rate = raw_input("What is it?")
    float(tx_rate)
    tx_rate = float(tx_rate)
    print "Thank you! Will use tax rate %s, instead of 0.15!" % (tx_rate)
else:
    print "Okay, that's allright, we'll use 0.15 as the average tax rate!"
    tx_rate = 0.15

That ensures that tx_rate is always defined.

Another solution that keeps repeating the question until the user enters yes or no (and thus also ensures that variable tx_data is defined) is:

tx_rate = None
while (not tx_rate):
    ans1 = raw_input("Do you know your tax rate?" )
    if ans1 == 'yes':
        tx_rate = raw_input("What is it?")
        float(tx_rate)
        tx_rate = float(tx_rate)
        print "Thank you! Will use tax rate %s, instead of 0.15!" % (tx_rate)
    elif ans1 == "no":
        print "Okay, that's alright, we'll use 0.15 as the average tax rate!"
        tx_rate = 0.15
    else:
        print "Sorry, incorrect value, please answer yes or no."

If you really want to be strict, you can add some range and type checking on the input for tx_rate. People could enter zero or a vlue larger than 1 or even text instead of a number.

Not the answer you're looking for? Browse other questions tagged or ask your own question.