-4

I just started coding today and I'm trying to write a simple program to add vectors. So far I have

VectorAx= input("What is the x component of Vector A?")
VectorAy= input("What is the y component of Vector A?")
VectorBx= input("What is the x component of Vector B?")
VectorBy= input("What is the y component of Vector B?")


VectorC= "[%s,%s]" % (VectorAx + VectorBx, VectorAy+VectorBy)
print (VectorC)

When I run the script everything works but the inputs aren't treated like numbers. For example, if VectorAx=1, VectorAy=6, VectorBx=3 and VectorBy=2, VectorC should be [4,8], but instead it is displayed as [13,62].

3 Answers3

2

input always returns a string object. If you want the inputs to be numbers, you need to convert them into numbers with either int or float:

VectorAx= int(input("What is the x component of Vector A?"))
VectorAy= int(input("What is the y component of Vector A?"))
VectorBx= int(input("What is the x component of Vector B?"))
VectorBy= int(input("What is the y component of Vector B?"))

Demo:

>>> inp1 = int(input(":"))
:1
>>> inp2 = int(input(":"))
:2
>>> inp1 + inp2
3
>>>
1

Cast your vectors to floats (if you plan on having decimals) or ints (if they will always be simple integers) then add.

Right now they are being taken in as strings.

Thus "1"+"3" == "13"

Whereas int("1") + int("3") == 4

Therefore:

VectorAx= int(input("What is the x component of Vector A?"))
VectorAy= int(input("What is the y component of Vector A?"))
VectorBx= int(input("What is the x component of Vector B?"))
VectorBy= int(input("What is the y component of Vector B?"))


VectorC= "[%s,%s]" % (VectorAx + VectorBx, VectorAy+VectorBy)

or you can simply cast here:

VectorC= "[%s,%s]" % (int(VectorAx) + int(VectorBx), int(VectorAy)+ int(VectorBy))
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
Tai
  • 1,206
  • 5
  • 23
  • 48
1

You'll want to use the built-in int() function.

Per the documentation, this function will "convert a number or string x to an integer, or return 0 if no arguments are given."

This converts input passed to it to an integer.

So, the resulting code should be:

VectorAx = int(input("What is the x component of Vector A?"))
VectorAy = int(input("What is the y component of Vector A?"))
VectorBx = int(input("What is the x component of Vector B?"))
VectorBy = int(input("What is the y component of Vector B?"))
okoboko
  • 4,332
  • 8
  • 40
  • 67
  • 3
    Yikes! Don't use `eval()` on user input! If anything, use `ast.literal_eval()` if necessary, but not for simple numbers. – Tim Pietzcker Jul 15 '14 at 18:19
  • Correct, my link however went to the `int` built-in function. – okoboko Jul 15 '14 at 18:21
  • Correct, but you're looking at `int` (the type) not `int` (the function) which is a built-in function according to https://docs.python.org/2/library/functions.html#int. – okoboko Jul 15 '14 at 18:23
  • 1
    To summarize. `int()` is just an invocation of `int` because both `functions` and `types/classes` are `callable` in Python. The documentation indeed mentions `int` as a built-in function. However the interpreter shows something else. `>>> type(int)` gives `` but `type(sorted)` gives ``. There seems to be some inconsistency in the documentation. – ElmoVanKielmo Jul 15 '14 at 18:28