473,666 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
#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.
Mar 25 '08 #1
1 9601
bvdet
2,851 Recognized Expert Moderator Specialist
#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:
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

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

Similar topics

2
1625
by: Martin Drautzburg | last post by:
Withing a module I can assign a value to a global var by assigning to it in the outermost scope. Fine. But how can I do this if the attribute name itself is kept in a variable. Once the module is loaded I can access the module's namespace no problem, but inside the module the dictionary is not yet present right ? IOW how can I write something like
5
3656
by: Rob | last post by:
When trying the basic tutorial for cgkit I always seem to get a not defined error as follows. Pythonwin GUI >>> from cgkit import * >>> Sphere() Traceback (most recent call last): File "<interactive input>", line 1, in ? NameError: name 'Sphere' is not defined
2
27575
by: jolly | last post by:
hey guys, When i try to run my code I get an error. NameError name 'main is not defined' if __name__ == "__main__": main() filename = "addbook.dat"
3
6353
by: willkab6 | last post by:
I am very new to both programming and Pyhton and while trying to do some practice using A byte of python an Error pops up on the IDLE shell. I am using windows XP. PLease see below. while running: guess = int(raw_input('Enter an integer : ')) if guess == number: print 'Congratulations, you guessed it.' running = False # this causes the while loop to stop elif guess < number:
1
30072
by: tshravan | last post by:
Hello All, I am newbie to python. I was trying to use a=zeros(5) in python shell, but it is throwing the following exception in the shell Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'zeros' is not defined
3
44540
by: Mathieu Prevot | last post by:
Hi I import subprocess and use Popen, but PIPE is not defined. I used 2.5.1, 2.5.2, Python 2.6a3+ (trunk:63576, May 24 2008, 12:13:40), it's always the same. What am I missing ? Thanks Mathieu
3
6566
by: cirfu | last post by:
im doing the python challenge and well, i solved this problem with ruby :) phrase = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj." puts phrase .tr('A-XY-Za-xy-z','C-ZA-Bc-za-b') the answer says to use maketrans but i cant get it to work:
4
3756
by: Harry | last post by:
Hi there. I am trying to download a file(sn.last) from a public FTP server with the following code: from ftplib import FTP ftp=FTP('tgftp.nws.noaa.gov') ftp.login() ftp.cwd('SL.us008001/DF.of/DC.radar/DS.81dpr/SI.kbuf') ftp.retrbinar('RETR sn.last', open(mydpa,'wb').write) ftp.quit()
4
2958
by: harijay | last post by:
Hi I am new to writing module and object oriented python code. I am trying to understand namespaces and classes in python. I have the following test case given in three files runner , master and child. I am getting an error within child where in one line it understands variable master.name and in the next line it gives a NameError as given here " print "Reset name now from %s to %s , oldname %s is saved in mastertrash" %...
0
8440
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8352
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8780
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7378
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4358
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.