Reputation:
I'm sorta new to python, and I wanted to try to create a program that finds the slope of two inputted points. I assume I'm doing fine on the math part, but the output part is my problem. This is my code:
import math
x1 = input("x1 = ")
y1 = input("y1 = ")
x2 = input("x2 = ")
y2 = input("y2 = ")
chy = y2 - y1
chx = x2 - x1
slope = 'Slope = ', chy, '/', chx
print (slope)
and this is my expected output:
x1 = 1
y1 = 1
x2 = 2
y2 = 2
Slope = 1/1
and this is my ACTUAL output:
x1 = 1
y1 = 1
x2 = 2
y2 = 2
('Slope = ', 1, '/', 1)
any help?
Upvotes: 1
Views: 82
Reputation: 2240
You want string concatenation:
slope = 'Slope = ' + str(chy) + '/' + str(chx)
The +
operator is used in Python to concatenate strings. However, it will give an error if you don't convert chy
and chx
to strings (by using str()
), since they currently have numerical values.
Another way to print strings is to use string formatting:
slope = 'Slope = {}/{}'.format(str(chy), str(chx))
Upvotes: 3
Reputation: 7100
Right now, slope
is a tuple (a sequence of Python objects), which is not what you want. What you want to do is
slope = 'Slope = ' + str(chy) + "/"+ str(chx)
This uses string concatenation (every part is a string, it is all put together).
Alternatively, you can use string formatting.
slope = "Slope = {}/{}".format(chy,chx)
Upvotes: 4