I just started to learn Python before a week or so and i found this example and rewrited it in Notepad++ (btw it was really painfull to figure out how to connect Notepad++ with python.exe but i like notepad++ more than Python GUI)
and this program just wont work nor in the Notepad++ nor in the Python GUI (i use 2.6 btw)

#  Calculator Program (Page 17 Python Tutorial)

# NO CODE IS REALLY RUN HERE,ITS ONLY TELLING US WHAT WE WILL DO LATTER

# Here we will define our functions
# This prints the main menu,and prompts for a choice
import time # Import time module - Its needed to pause program to see result

def menu():
	# print what options we have
	print "Welcome to the calculator.py"
	print "Your options are:"
	print " "
	print "1) Addition"
	print "2) Substraction"
	print "3) Multiplication"
	print "4) Division"
	print "5) Quit Calculatory.py"
	print " "
	return raw_input("Choose your option: ")
	
# This add two numbers given
def add(a,b):
	print a ,"+", b , "=" , a + b
	
# This substracts two numbers
def sub(a,b):
	print a ,"-" , b ,"=" , a-b
	
# This multiplies two numbers
def mul(a,b):
	print a ,"*", b , "=" , a*b

# This divides two numbers
def div(a,b):
	print a , "/" , b ,"=" , a/b

# NOW THE PROGRAM REALLY STARTS,AS CODE IS RUN

loop = 1 
choice = 0

while loop == 1:
	choice = menu()
	if choice == 1:
	add(raw_input("Add this: "),raw_input("to this: "))
	elif choice == 2:
	sub(raw_input("Subdivide this: "),raw_input("by this: "))
	elif choice == 3:
	mul(raw_input("Multiply this: "),raw_input("by this: "))
	elif choice == 4:
	div(raw_input("Divide this: "),raw_input("by this: "))
	elif choice == 5:
	loop = 0
	print "Thank you for using Calculator.py"
	
raw_input("Press ENTER to exit")
time.sleep(5)

i get error message at add(raw_input("Add this: "),raw_input("to this: "))

The code is exactly as in tutorial :/
thx in advance
regards Perun

Recommended Answers

All 7 Replies

In the "if choice" sequence, you should replace raw_input by input because you want the user input to be interpreted as a number and not as a string. Also read this about how to post python code in this forum.

Or if you stay with raw_input,you can convert to integer later in the function.

def add(a,b):    
    print a, '+', b, '=', int(a) + int(b)
    #print '%s + %s = %d ' % (a, b, int(a)+int(b))  # Try this line to

choice = menu() So now choice take take the variable that get returned for menu() function.
So if user choice 1,what will choice be integer or string?

Choose your option: 1

>>>choice = menu()
>>> choice
'1'

Yes it return a string.

Will this line work correct then? if choice == 1:

>>> '1' == 1
False
>>> 1 == 1
True
>>>

So now you now what to change in that line.

how to connect Notepad++ with python.exe but i like notepad++ more than Python GUI)

Use a edtior that that you can just run your code on the fly.
Just paste inn code and push run button.
Pyscripter is god.
http://code.google.com/p/pyscripter/

And use code tag when you post code.

Let's hope that the code you are showing is not exactly like in the tutorial. There are some major mistakes in your code. Also, please do not use tabs for indentations! In most editors tabs come out as 8 spaces and make the code look rather messy. I don't even want to bother looking at it.

BTW, any good Python editor would have alerted you of some of the coding mistakes you made.

Member Avatar for masterofpuppets

hi
you have a few problems here:
first, in the menu function if you return a string, you cannot check for integer choice in the main loop.
second, in the main loop the arguments for the functions have to be integers or floats so better change the raw_input to input:

def menu():
    # print what options we have
    print "Your options are:"
    print " "
    print "1) Addition"
    print "2) Substraction"
    print "3) Multiplication"
    print "4) Division"
    print "5) Quit Calculatory.py"
    print " "

    return input( "Choose your option: " )

#calc functions add(), sub......
...
...
 
goOn = True
print "Welcome to the calculator.py"

while goOn:
    choice = menu()
    if choice == 1:
        a, b = input( "Add: " ), input( "and: " )
        add( a, b )
    elif choice == 2:
        a, b = input( "Subtract: " ), input( "and: " )
        sub( a, b )
    elif choice == 3:
        a, b = input( "Multiply: " ), input( "by: " )
        mul( a, b )
    elif choice == 4:
        a, b = input( "Divide: " ), input( "by: " )
        div( a, b )
    elif choice == 5:
        goOn = False
    print
    
print "Thank you for using Calculator.py"

also put the 'thank you....' message outside the loop in order to see it only once :)

Member Avatar for masterofpuppets

Or if you stay with raw_input,you can convert to integer later in the function.

def add(a,b):    
    print a, '+', b, '=', int(a) + int(b)
    #print '%s + %s = %d ' % (a, b, int(a)+int(b))  # Try this line to

choice = menu() So now choice take take the variable that get returned for menu() function.
So if user choice 1,what will choice be integer or string?

Choose your option: 1

>>>choice = menu()
>>> choice
'1'

Yes it return a string.

Will this line work correct then? if choice == 1:

>>> '1' == 1
False
>>> 1 == 1
True
>>>

So now you now what to change in that line.


Use a edtior that that you can just run your code on the fly.
Just paste inn code and push run button.
Pyscripter is god.
http://code.google.com/p/pyscripter/

And use code tag when you post code.

ah right, sorry dude my post is basically what you posted as well...I didn't read it :) :)

Thank you guys on help, and thank you for that ling for PyScripter i like it alot. And yes that tutorial is the same i just added little spacing here and there to see more easy what im doing.

i just added little spacing here and there to see more easy what im doing.

Always use 4 space for indent.
Read PEP 8.
http://www.python.org/dev/peps/pep-0008/

Tutorial look like it come from sthurlow
http://www.sthurlow.com/python/lesson05/

And some strange code confusing for beginner.

add(raw_input("Add this: "),raw_input("to this: "))

This call function add with 2 argument.

So a more simpler way to understand this.

>>> def add(a,b):    
    print a, '+', b, '=', int(a) + int(b)

>>> a = raw_input("Add this: ")
Add this: 5
>>> b = raw_input("to this: ")
to this: 9
>>> 
>>> # now let us call add() function in a more normal way
>>> add(a, b)
5 + 9 = 14
>>>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.