ColoradoKing ColoradoKing - 24 days ago 16
Python Question

Create asterisk box given rows and columns in main [python]

I'm changing the question as it was poorly worded with no code.
Here is the question.
I ask the user to input the rows and columns in the main function. I then send the input into another function that creates a box of asterisks with given rows and columns, then returns it to be used later on in the main.
This is what I have.

def main():
rows = int(input("Enter rows :: "))
columns = int(input("Enter cols :: "))
myBox = makeBox(rows,columns)

def makeBox(r,c):
for i in range(r):
return("*"*c)

main()

Answer

Do you mean something like this

rows = 4
columns = 7

for i in range(rows):
    print("*"*columns)

This will output

*******
*******
*******
*******

I can't answer why to do it this way. I could explain each row of code though.

[EDIT] How to return the asterix box from a function (there are better ways but this works)

rows = 4
columns = 7

def get_table(r,c):
    table = ""
    for i in range(rows):
        table += '*'*columns+'\n'
    return table

print(get_table(rows,columns))

The get_table function returns a string that contains the entire table. Notice the '\n', this indicates the character for a new line.

Comments