By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

NameError: name 'y' is not defined is a problem i am facing?

P: 1
#This is to add, sub, mul and div using functions


def menu():
print ""
print "Welcome to the calculator"
print "===== These are some of the options ====="
print "1--> for Addition"
print "2--> for Subtraction"
print "3--> for muliplication"
print "4--> for Division"
print "5--> To QUIT"
return input("Enter any of the above options: ")

def toexit():
print ""
a=input("Press y to continue or n to exit: ")
y=1
print a
if a==1:
return
elif a==0:
exit()

def add(a,b):
print a,"+",b,"=",a+b
toexit()
def sub(a,b):
print a,"-",b,"=",a-b
toexit()
def mul(a,b):
print a,"*",b,"=",a*b
toexit()
def div(a,b):
print a,"/",b,"=",a/b
toexit()


loop=1
choice=0
while loop==1:
choice=menu()
if choice==1:
add(input("Add this: "),input("to this: "))
elif choice==2:
sub(input("Sub this: "), input("from this: "))
elif choice==3:
mul(input("Mul this: "),input("with this: "))
elif choice==4:
div(input("Div this: "),input("with this: "))
elif choice==5:
loop=0
else:
loop=0
print ""
print "Thank u for using CALCULATOR"

---------------------------------------------------------------------------------
Hello All,
I am new learner of python, it is really an interesting language.Can u guyz hepl me out.

When i run this script, i will enter any one of the option, then enter 2 nos.
Then i used the function to ask or prompt for "Do u want to continue Y/N"; there u have to press either "y" or "n".
If "y" is pressed the options must be displayed
else if "n" is pressed the progarm must exit.

Tthe error i am getting is:
---------------------------------------------------------------------------------
a=input("Press y to continue or n to exit: ")
File "<string>", line 1, in <module>
NameError: name 'y' is not defined
---------------------------------------------------------------------------------


Can any1 say wht is the problem?
Plz giv me the solution.
Mar 25 '08 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
#This is to add, sub, mul and div using functions


def menu():
print ""
print "Welcome to the calculator"
print "===== These are some of the options ====="
print "1--> for Addition"
print "2--> for Subtraction"
print "3--> for muliplication"
print "4--> for Division"
print "5--> To QUIT"
return input("Enter any of the above options: ")

def toexit():
print ""
a=input("Press y to continue or n to exit: ")
y=1
print a
if a==1:
return
elif a==0:
exit()

def add(a,b):
print a,"+",b,"=",a+b
toexit()
def sub(a,b):
print a,"-",b,"=",a-b
toexit()
def mul(a,b):
print a,"*",b,"=",a*b
toexit()
def div(a,b):
print a,"/",b,"=",a/b
toexit()


loop=1
choice=0
while loop==1:
choice=menu()
if choice==1:
add(input("Add this: "),input("to this: "))
elif choice==2:
sub(input("Sub this: "), input("from this: "))
elif choice==3:
mul(input("Mul this: "),input("with this: "))
elif choice==4:
div(input("Div this: "),input("with this: "))
elif choice==5:
loop=0
else:
loop=0
print ""
print "Thank u for using CALCULATOR"

---------------------------------------------------------------------------------
Hello All,
I am new learner of python, it is really an interesting language.Can u guyz hepl me out.

When i run this script, i will enter any one of the option, then enter 2 nos.
Then i used the function to ask or prompt for "Do u want to continue Y/N"; there u have to press either "y" or "n".
If "y" is pressed the options must be displayed
else if "n" is pressed the progarm must exit.

Tthe error i am getting is:
---------------------------------------------------------------------------------
a=input("Press y to continue or n to exit: ")
File "<string>", line 1, in <module>
NameError: name 'y' is not defined
---------------------------------------------------------------------------------


Can any1 say wht is the problem?
Plz giv me the solution.
Please use code tags when posting code. Please read "How to post a question".

Do not use built-in function input(). Code could inadvertently execute, which could cause havoc. Use raw_input() instead.

Try the following:
Expand|Select|Wrap|Line Numbers
  1.  
  2. def toexit():
  3.     print ""
  4.     a=raw_input("Press y to continue or n to exit: ")
  5.     if a.lower().strip() != 'y':
  6.         # call your code to exit the script
  7.         # any key other than 'y' will exit
Mar 25 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.