6

So I just started learning how to code (completely new at this) and I decided to go with Python... So I recently am learning how to use functions to do math and I was making my own "coding" to see if I can come up with the result I want which is use functions to add x + y and give me a result but I keep getting the literal x + y and not the sum of those two numbers. eg. 1 + 1 = 11 (instead of 2)

Below is the code, can anyone please tell me what I am doing wrong. Thanks!~ (and yes, I am using a book but it is somehow vague on the explanations [Learn Python the Hard Way])

def add(a, b):
    print "adding all items"
    return a + b

fruits = raw_input("Please write the number of fruits you have \n> ")
beverages = raw_input("Please write the number of beverages you have \n> ")

all_items = add(fruits, beverages)
print all_items

FYI, the code the book gave me was:

    def add(a, b):
    print "ADDING %d + %d" % (a, b)
    return a + b

def subtract(a, b):
    print "SUBTRACTING %d - %d" % (a, b)
    return a - b

def multiply(a, b):
print "MULTIPLYING %d * %d" % (a, b)
return a * b

def divide(a, b):
    print "DIVIDING %d / %d" % (a, b)
    return a / b


 print "Let's do some math with just functions!"

age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)

print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)

# puzzle
print "Here is a puzzle."

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

print "that becomes: ", what, "Can you do it by hand?"
Eric Ahn
  • 155
  • 1
  • 2
  • 11
  • it is supposed to stick in this way because what you put into `raw_input()` is a string. Check and convert it to integer – Tanmaya Meher Oct 22 '17 at 17:22
  • `raw_input` sends the input as string format you need to typecast it to integer, i.e. `fruits = int(raw_input("Please write the number of fruits you have \n> "))` – Arpit Goyal Oct 22 '17 at 17:22
  • man that was fast answering, thanks a lot that solved my problem, i havent been taught really how to use integer (i have but it was so vague i didnt fully understand)..cheers :) – Eric Ahn Oct 22 '17 at 17:27
  • Yes, you can add a lot of different stuff in Python: lists, tuples, strings, integers, floats, anything that has the `__add__` magic method. – ForceBru Oct 22 '17 at 20:13

3 Answers3

11

In python (and a lot of other languages), the + operator serves a dual purpose. It can be used to get the sum of two numbers (number + number), or concatenate strings (string + string). Concatenate here means join together.

When you use raw_input, you get back the user's input in the form of a string. Thus, doing fruits + beverages invokes the latter meaning of +, which is string concatenation.

To treat the user's input as a number, simply use the built-in int() function:

all_items = add(int(fruits), int(beverages))

int() here converts both strings to integers. Those numbers are then passed to add(). Keep in mind that unless you implement a check to make sure that the user has inputted a number, invalid input will cause a ValueError.

stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
  • 1
    thanks that helped me understand it even better – Eric Ahn Oct 22 '17 at 17:28
  • @EricAhn Glad I could help. Be sure to mark the answer as [accepted](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) once the waiting period is over, and best of luck. – stelioslogothetis Oct 22 '17 at 17:29
  • Minor quibble: I would say the word "cast" is incorrect here, because you actually want to *convert* the value into an `int` object, not simply *cast* it. The Python language doesn't really have typecasting like some other languages do. The `int()` "function" here is actually the constructor for the `int` class (which is a built-in type in Python), which is the class of integer number values; by calling `int()` with a string argument, you pass that string into the `int` constructor and get a corresponding `int` object. Just a point of terminology -- otherwise, this answer is totally correct. – Daniel Pryden Oct 22 '17 at 19:37
3

The '+' operator can be a concatenation operator for strings, lists etc. and an addition operator for numbers. Try adding int() wrappers to your inputs. Also you may see the type of a variable via type()

nettrino
  • 588
  • 6
  • 21
2

The raw_input function returns a string, not a number. The + operator, when used on strings, concatenates them.

You need to parse the strings into numbers using int() or float() on the result.

Chris R. Donnelly
  • 3,086
  • 3
  • 28
  • 28