0

This is my code. How do I make bownew = multiple rows of *'s? I do not want to just print for x in range(height):print bownew. I want bownew to be equal to the for x in range(height). How can this be done?

height = int(input("Enter an odd number greater than 4: "));
column = height * 2;

screen = [];
bownew = ""

def bow(height):
    for x in range(height):
        screen.append(["*"]*column);

bow(height);

for i in screen:
    bownew = " ".join(i)
print(bownew)
3
  • There is no need for semi-colons in Python (at least how you are using them). Commented Sep 5, 2014 at 2:14
  • is it just to have multiple code lines on one line? Commented Sep 5, 2014 at 2:19
  • @paulandshadow Yes, that's the only time they're necessary. Commented Sep 5, 2014 at 2:26

2 Answers 2

2

Don't use a for loop, join wants the list as the argument. And if you want it to have multiple lines, join them with newlines.

bownew = "\n".join(screen)

You also need to make screen a list of strings, not a list of lists:

def bow(height):
    for x in range(height):
        screen.append("*" * column);

The whole script:

height = int(input("Enter an odd number greater than 4: "));
column = height * 2;

screen = [];
bownew = ""

def bow(height):
    for x in range(height):
        screen.append("*" * column);

bow(height);

bownew = "\n".join(screen)

print(bownew)

Test run:

$ python test.py
Enter an odd number greater than 4: 5
**********
**********
**********
**********
**********
Sign up to request clarification or add additional context in comments.

14 Comments

but i want there to be multiple rows of that. There should be the inputed height variable as the number of rows
Join them with newlines instead of spaces.
it does not work. Says expected string but got list.
I think the problem is the way you create screen. It's a list of lists, not a list of strings. Try "*"*column instead of ["*"]*column
still not working "list obj cannot be interp. as int"
|
0

1.There might be a simpler solution, that requires no use of for loop

height = int('Enter an odd number greater than 4: ')
column = height * 2

row = '*' * column
bownew = [row] * height
bownew = '\n'.join(bownew)

print(bownew)

,and I have tested it, it works.

Enter an odd number greater than 4: 5
**********
**********
**********
**********
**********

2. As for the question(How to set variable equal to for loop in python?), I suspect that you can not make a variable equal to any loop unless you use def .

The use of def will be

height = int('Enter an odd number greater than 4: ')
column = height * 2

def bownew(height, column):
    for i in range(height):
        print('*' * column)

bownew(height, column)

This should give you the same result.

Happy coding, hope this is helpful.

1 Comment

Welcome to StackOverflow! Please enter your code directly in your answer instead of in an image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.