Reputation: 85
I need help fixing my code, see below for problem.
I have to write a function that prints out the L shape made up of *'s. Also, the parameter M is an integer between 1 and 80 and is the rows of the shape. The output needs to be N rows, the last of which should comprise N letters of L. My user input should be the desired rows but its just printing the range, and even that looks a little off. Please assist, if you can... this is what I have so far:
M = int(input())
def solution(M):
result =""
if M > 80:
for row in range(1, 80):
for column in range(1, 80):
if (column == 1 or (row == 1 and column != 0 and column < 1)):
result = result + "*"
else:
result = result + "\n"
print(result)
solution(M)
Upvotes: 0
Views: 116
Reputation: 225
You can try with this solution. I hope that it's clear enough:
SYMBOL = '*'
def solution(M, N):
result = []
# validations
if M > 80:
M = 80
if N > 80:
N = 80
for row in range(1, M + 1):
if row < M:
result.append(SYMBOL)
else:
# last row
result.append(''.join([SYMBOL for column in range(1, N + 1)]))
return result
# generate rows
result = solution(10, 5) # TODO: use values from input() instead
# print result
for row in result:
print(row)
Upvotes: 0
Reputation: 5359
This is just printing * for each row, unless the row = M in which case it is printing "*" * M and exiting if the value is >= 80 or <= 1 (since you said between 1-80):
import sys
print("Please enter an integer between 1-80:")
M = int(input())
def solution(N):
if N >= 80 or N <= 1:
sys.exit("Please run again using an integer between 1-80")
result = ""
for row in range(1, N+1):
if row < N:
result = "*"
print(result)
elif row == N:
result = "*" * N
print(result)
solution(M)
Then when you run:
[dkennetz@nodecn001 tmp]$ python3 fun.py
Please enter an integer between 1-80:
4
*
*
*
****
Upvotes: 1
Reputation: 1181
M = int(input())
def solution(M):
result_str=""
for row in range(0,M+1):
for column in range(0,M+1):
if (column == 1 or (row == M and column != 0 and column < M)):
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n"
print(result_str)
solution(M)
https://www.w3resource.com/python-exercises/python-conditional-exercise-21.php
Upvotes: 1