Reputation: 453
I thought I was copying word for word from a previous question/answer, but, I don't get what I wanted.
I was trying to answer a question from an online list of python questions for beginners. I wrote the following in a file called q.py
and then gave the executed it with python3 q.py
in my shell.
The file q.py
looks like:
from math import sqrt
C=50
H=30
inp = input('input values: ')
vals = inp.split(',')
print(vals)
for x in vals:
D=int(x)
print(int(sqrt(2*C*D/H)),sep=',',end='')
print()
I input 100,150,180
as the question suggested, the required answer is:
18,22,24
but the answer I get is:
182224
When I omit the "end"
argument to print
, I get 18
, 22
, and 24
separated by newlines.
Upvotes: 0
Views: 1245
Reputation: 160407
sep
is used to separate values if multiple values are actually provided; you provide one value for every loop so the separator isn't used.
Instead of using sep=','
, use end=','
to get the desired effect:
for x in vals:
D=int(x)
print(int(sqrt(2*C*D/H)),end=',')
print()
Using similar input, the result printed out is:
['100', '150', '180']
18,22,24,
If, for example, you provided multiple values, say, by unpacking a list comprehension in your print
call, sep=','
would work like a charm:
print(*[int(sqrt(2*C*int(x)/H)) for x in vals], sep=',')
Prints:
18,22,24
Upvotes: 2