Looking for help, just started with Python today. (3.0)
I'm just trying to get into python, but I found it very difficult to find any resource that is Python 3. All I have so far is diveintopython3.org and its limitation. Anyway, I was just trying to understand the language by doing some very simple things, 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 this, but any other resources containing 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)
exit:
Enter a number: 23
Enter a number: 24
23
24
a source to share
The Python Byte details Python 3. There is also a 2.X version of the book that can help you compare and contrast the differences in languages.
To fix the problem, you need to convert your input to an integer. By default, it is stored as a string.
num = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
a source to share
You didn't say what you got - I'm assuming num
and num2
concatenate as the input
string returns. Adding two lines just concatenates them. If you expect num
and num2
represent integers, you can use int
to convert strings to integers:
num = int(input("Enter a number:")
num2 = int(input("Enter a number:")
And you will most likely get better results. Note that there is still room for better error checking, but that might get you started.
Another thing to try is add a line at the end of yours __main__
like this:
add(4, 3)
and see what is printed. This will tell you if there is a mistake with add
or with your input routines.
Surely none of this provided you with a resource - are these interactive documents that don't help? I would start with a tutorial if you haven't already.
a source to share
Interesting, 3 answers and none of them fixes your problem correctly.
All you have to do is the following:
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
a source to share
There, the Addison-Wesley book by Mark Summerfield is called Programming in Python 3, and I found it to be the best Python book I have read. One of the nice things for you, I think, is that Summerfield doesn't differentiate between 2.X and 3.x, so someone just picks up Python, gets a continuous view of (new and improved) Python. Add to that that he explains things that other books - in my case from 1.X - either never touched upon, or (I think) weren't wrong. In the custom exception paragraphs, I just pulled me out of the jam, and his handling of * and ** as unpacking operators cleared a lot of mental fog for me. The most first-class book.
By the way, there is a module called sys that does useful things, such as allowing you to access command line arguments. These arguments are (under) strings and the other day I had to use int () as commenter dkbits says to use them. (The type () function tells you what type Python considers a variable.) 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)
a source to share