Issue
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
Solution
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: "))
Answered By - Dave K
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.