Looking for help, just started with Python today. (3.0)
Question:
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
Answers:
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: "))
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.
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)
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
Python considers the input as a string. To make python interpret the input as an integer (number), add int()
around input("Enter a number:")
.
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
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: "))
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.
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)
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
Python considers the input as a string. To make python interpret the input as an integer (number), add int()
around input("Enter a number:")
.