-1

I'm working on an assignment where I have to code a program where the input and output are shown below:

Input: 1 2 3 4 5

Desired Output:

1| 2| 3| 4| 5
2| 4| 6| 8|10
3| 6| 9|12|15

I can't figure out how to properly do this but here is all my code so far:

row1 = [1*int(i) for i in input().split()]

row2 = [2*i for i in row1]
row3 = [3*i for i in row1]

row1= str(row1)
row1= '|'.join(row1)[2:-2]

row2 = str(row2)
row2= '|'.join(row2)[2:-2]

row3 = str(row3)
row3= '|'.join(row3)[2:-2]

my_list = [row1, row2, row3]

print(row1)
print(row2)
print(row3)

The output of my code is this which obviously isn't the desired output:

1|,| |2|,| |3|,| |4|,| |5
2|,| |4|,| |6|,| |8|,| |1|0
3|,| |6|,| |9|,| |1|2|,| |1|5

Please help me get the desired output which as shown above

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Luke
  • 1
  • Welcome to Stack Overflow! Please take the [tour] and read [ask]. SO is a Q&A site, but this is not a question. You can [edit] to turn it into one. – wjandrea Oct 09 '21 at 22:22
  • See how to create a [mcve]. – Peter Wood Oct 09 '21 at 22:24
  • I will give you a hint though: `str(row)` is a step in the wrong direction – wjandrea Oct 09 '21 at 22:24
  • Sidenote: `my_list` is unused – wjandrea Oct 09 '21 at 22:25
  • On second thought, you can probably find a solution in these existing questions: [How can I fill out a Python string with spaces?](/q/5676646/4518341), [How to concatenate items in a list to a single string?](https://stackoverflow.com/q/12453580/4518341) And it seems like you already know how to convert a list of ints to a list of strings. – wjandrea Oct 09 '21 at 22:37

1 Answers1

0

The issue you are having is that you are converting the numbers to string, and then joining each character inside with a |. Just don't convert them to string. Keep them as list of integers. Also I can't see how you can possibly get your result using [2:-2].

Stefano
  • 1,686
  • 1
  • 16
  • 25