Computer Science, asked by Anonymous, 4 months ago

Correct this python cøde to print the output as shown in the attachment:

L = [ "P", "Y", "T", "H", "O", "N" ]
a = 0
for j in range (1, 7):
for i in range (j):
print(L[a], end = " ")
a = a + 1
print()

Attachments:

Answers

Answered by rohan15086
1

Explanation:

Sodium carbonate is a chemical compound with the molecular formula Na2CO3.

Answered by anindyaadhikari13
3

\texttt{\textsf{\Large{\underline{Solution}:}}}

\texttt{\textsf{\large{\underline{Given C{o}de}:}}}

L = [ "P", "Y", "T", "H", "O", "N" ]

a = 0

for j in range (1, 7):

for i in range (j):

print(L[a], end = " ")

a = a + 1

print()

\texttt{\textsf{\large{\underline{Corrected C{o}de}:}}}

L = ["P", "Y", "T", "H", "O", "N"]

for j in range (1, 7):

   a=0

   for i in range (j):

       print(L[a], end = " ")

       a = a + 1

   print()

\texttt{\textsf{\Large{\underline{Errors Corrected}:}}}

  • Indentation not given. Its mandatory to give indentation.
  • On moving to next row, you have to start from first element of list 'L'. So, for that, you have to change the value of 'a' to 0 otherwise IndexError will occur. So, here, I wrote a = 0 before the starting of inner loop.

\texttt{\textsf{\Large{\underline{Other Approaches}:}}}

You can also use these approaches to solve this problem.

1. List Slicing.

L = [ "P", "Y", "T", "H", "O", "N" ]

for j in range (1, 7):

   print(*L[:j],sep=" ")

See the attachments for output.

Attachments:
Similar questions