I have this code to print ‘H’ in a pattern using ‘*’:
def grid_h():
result_str='';
for row in range(5):
for column in range(5):
if((column == 0 or column == 4)
or (row == 2)):
result_str = result_str + '*'
else:
result_str = result_str + ' '
result_str = result_str + '\n'
return result_str
I’m trying to print HH in the same line but it always go to the new line.
I know there is an end=’‘ at Python 3 to stay in the same line but I have tried this and still not working: print(grid_h(), end=’‘), print(grid_h())
It looks like you are trying to print the ‘H’ pattern twice on the same line. The print(grid_h(), end='')
and print(grid_h())
statements are on separate lines, and print(grid_h())
will start a new line.
To print ‘H’ patterns twice on the same line, you can concatenate the two result_str
strings and then print the combined string. Here’s an example:
def grid_h():
result_str = ''
for row in range(5):
for column in range(5):
if (column == 0 or column == 4) or (row == 2):
result_str += '*'
else:
result_str += ' '
result_str += '\n'
return result_str
# Concatenate two 'H' patterns and print on the same line
combined_pattern = grid_h() + grid_h()
print(combined_pattern, end='')
This way, combined_pattern
will contain two ‘H’ patterns, and print(combined_pattern, end='')
will print them on the same line.