views:

158

answers:

4

I am just trying to get into python, but I've found it very difficult to find any resource that is Python 3. All I've got so far is diveintopython3.org, and its limited. Anyways, I was just trying to get a feel for the language by doing some very basic stuff, but I can't figure out why this little program won't do what I intend, which is to add 2 numbers. I'm sure someone here knows how to fix it, but any other resources that contain tutorials in Python 3 would be greatly appreciated:

def add(num=0,num2=0):
    sumEm = (num+num2)
    print (sumEm)

if __name__ == '__main__':
    num = input("Enter a number: ")
    num2 = input("Enter a number: ")
    add(num,num2)

output:

Enter a number: 23
Enter a number: 24
23
24
+7  A: 

A Byte of Python covers Python 3 in detail. There's also a 2.X version of the book, which can help compare and contrast the differences in the languages.

To fix your problem, you need to convert the input taken into an integer. It's stored as a string by default.

num = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
Dave K
Ah, thanks for the link and code. But now I don't understand, I thought one of the reasons people hailed python was because it just knew what type of variable it was. This seems like you're just declaring what type of variable it is in a different place than you would in java or c++.
Justen
It does know what type of variable the input() function returns. It's just that it's different from your expectations.
Craig McQueen
To expand on Craig's comment: Python's type handling doesn't read your mind and figure out that you meant the numbers 23 and 24 rather then the strings '23' and '24'. The function input always returns a string. The touted nice thing about Python's type handling is that your function add would do the 'right' thing for whatever parameters you passed in. If you passed in two ints you'd have gotten an int back. If you passed in two floats you'd have gotten a float back. In this case you passed in two strings, and the string plus operator concatenates the two strings.
Charles E. Grant
+3  A: 

You didn't say what you do get - I'm guessing num and num2 concatenated, as the input returns a string. Adding two strings just concatenates them. If you expect num and num2 to represent integers, you could use int to convert the strings into integers:

num = int(input("Enter a number:")
num2 = int(input("Enter a number:")

And you'll likely get better results. Note there's still room for better error-checking, but this might get you started.

One other thing to try - add a line at the end of your __main__ like this:

add(4, 3)

and see what gets printed. That will tell you whether the fault is with add or with your input routines.

Of course, none of that provided you with a resource - are the online docs not helping? I'd start with the tutorial, if you haven't already.

Blair Conrad
A: 

There's an Addison-Wesley book by Mark Summerfield called "Programming in Python 3", and I have found it to be the best Python book I've read. One nice thing for you, I would imagine, is that Summerfield does not bring up differences between 2.X and 3.x, so someone just picking up Python gets an uninterrupted view of (new and improved) Python. Add to this that he explains things that the other books --- in my case from 1.X --- either never touched or (I think) misstated. The paragraphs on custom exceptions just got me out of a jam, and his treatment of * and ** as unpacking operators cleared up considerable mental fog for me. Top-notch book.

BTW, there's a module called sys that does useful things, like let you access command line arguments. Those args are (sub)strings, and the other day I had to use int(), as commenter dkbits says, to put them to use. (The type() function tells you what type Python considers a variable to be.) I had:

import sys

#Parse the command line.
if len(sys.argv) != 4:
    print "Usage: commandName maxValueInCell targetSum nCellsInGroup"
    exit()
else:
    maxv = int( sys.argv[1])
    tgt = int( sys.argv[2])
    nc = int( sys.argv[3])
print "maxv =",maxv, "; tgt = ",tgt, "; nc = ",nc
print type(sys.argv[1])   #strings
print type(sys.argv[2])
print type(sys.argv[3])
print type(maxv)          #ints
print type(tgt)
print type(nc)
behindthefall
A: 

Interesting, 3 answers, and none of them address your problem correctly.

All you have to do is this:

def add(num=0,num2=0):
    sumEm = (int(num)+int(num2)) # may need the int() because in python 3.0 the manual says it only returns strings
    return sumEm # use return here not print
Unknown