in Education by
I need to create a hollow pyramid with a solid top layer like so: Height: 5 * *** * * * * ********* Also needs a user input for height/number of rows, so it can't just be hardcoded, for example with a height of 4: * *** * * ******* Using this code I received this output with no solid layer on top: * * * * * * * ********* n = 5 for i in range(n): for j in range(n - i - 1): print(' ', end='') for k in range(2 * i + 1): if k == 0 or k == 2 * i: print('*', end='') else: if i == n - 1: print('*', end='') else: print(' ', end='') print() JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Here's a modified version of your code that prints the triangle correctly. Note the change to the if statements. n = 5 for i in range(n): for j in range(n - i - 1): print(' ', end='') for k in range(2 * i + 1): if k in [0,2*i]: print('*', end='') elif i in [1,n-1]: print('*', end='') else: print(' ', end='') print() Alternatively, here's a "one line" solution. n = 5 print('\n'.join([' '*(n-i-1)+'*'*(2*i+1) if i in [0,1,n-1] else (' '*(n-i-1)+'*'+' '*(2*i-1)+'*') for i in range(n)]))

Related questions

0 votes
    I am currently trying to understand the architecture behind the word2vec neural net learning algorithm, for representing ... page 67. Select the correct answer from above options...
asked Jan 29, 2022 in Education by JackTerrance
0 votes
    I want different learning layers in different layers just like we do in Caffe. I just want to speed up the training ... can I do this? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I'm starting from the pandas DataFrame docs here: http://pandas.pydata.org/pandas-docs/stable/dsintro.html I'd ... =1)] print valdict Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    Is this possible? From what I'm seeing, the only way to get options into the the python class is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    How can I remove emojis that start with '\x' when reading a csv file using pandas in Python? The ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I could swear I've seen the function (or method) that takes a list, like this [3, 7, 19] ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I could swear I've seen the function (or method) that takes a list, like this [3, 7, 19] ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I am using Eclipse with Pydev and googleAppengine. I have python 2.7 installed. I am trying to run ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I am getting an 'access is denied' error when I attempt to delete a folder that is not empty. I used the ... that is not empty? Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    I want to change a couple of files at one time, iff I can write to all of them. I'm wondering if I ... to this problem look like? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I've always laughed to myself when I've looked back at my VB6 days and thought, "What modern language doesn't ... ++/-- notation? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I want to create a new list from the existing two lists that will have all the matches from list 2 which has an exact ... *", i) in a] Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    I'm new to Python and I want to install some packages with pip. But pip install unroll gives me: Command " ... How to solve this? Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    I am a learner and just want to know the basic concept of super(). Here in the given code I want to know ... () ChildOne() ChildTwo() Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    Can someone tell me how to find all the files in directory with .txt extension in Python? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
...