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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manojnkumar
    New Member
    • Mar 2008
    • 1

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

    #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("t o this: "))
    elif choice==2:
    sub(input("Sub this: "), input("from this: "))
    elif choice==3:
    mul(input("Mul this: "),input("w ith this: "))
    elif choice==4:
    div(input("Div this: "),input("w ith 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.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by manojnkumar
    #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("t o this: "))
    elif choice==2:
    sub(input("Sub this: "), input("from this: "))
    elif choice==3:
    mul(input("Mul this: "),input("w ith this: "))
    elif choice==4:
    div(input("Div this: "),input("w ith 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:[code=Python]

    def toexit():
    print ""
    a=raw_input("Pr ess y to continue or n to exit: ")
    if a.lower().strip () != 'y':
    # call your code to exit the script
    # any key other than 'y' will exit[/code]

    Comment

    Working...