18.5k views
0 votes
What is wrong with this code and correct it.

def swapping_stars():

line_str = ""

for line in range(0, 6):

for char in range(0,6):

if line % 2 == char % 2:

line_str += "*"

else:

line_str += "-"

print(line_str)

1 Answer

6 votes

Answer:

What's wrong?

The line_str variable needs to be re-initialized at the end of each printing

The correction

Include the following line immediately after the print statement

line_str = ""

Step-by-step explanation:

Required

Correct the code

The conditions are correct. However, the string variable line_str needs to be set to an empty string at the end of every of the iteration loop.

This will allow the code to generate a new string.

So, the complete function is:

def swapping_stars():

line_str = ""

for line in range(0, 6):

for char in range(0,6):

if line % 2 == char%2:

line_str += "*"

else:

line_str += "-"

print(line_str)

line_str = ""

User Dushyantp
by
4.6k points