#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.
1 9585 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("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: -
-
def toexit():
-
print ""
-
a=raw_input("Press 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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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"
|
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:...
|
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...
|
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...
|
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...
|
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()...
|
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...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |