Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3349832
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:42:48+00:00 2026-05-18T01:42:48+00:00

I am currently working on a homework assignment to generate what is known as

  • 0

I am currently working on a homework assignment to generate what is known as Pascal’s triangle in Python.

So far, this is what I have:

def mytri(myrange):
    trianglevar = [[1]]
    for i in range(1, myrange):
        tempvar = [1]
        for n in range(0, i-1):
            tempvar.append(trianglevar[i-1][n]+trianglevar[i-1][n+1])
        tempvar.append(1)
        trianglevar.append(tempvar)
    return trianglevar

def mymenu():
    for i in mytri(int(raw_input("Please enter the height of the triangle: "))):
        print i
    print '\n'
    choicevar = raw_input("Would you like to create another triangle? (y/n): ")
    if choicevar == "y":
        mymenu()
    else:
        print "Goodbye."

mymenu()

What the program does up to this point is perform the calculation for the triangle. It calculates the numbers in each row (starting with 1), and stops after reaching the number of rows specified by the user.

However, I’m not sure how to format my triangle. It currently prints as:

[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
...etc.

The output I want is:

        [1]
      [1, 1]
    [1, 2, 1]
  [1, 3, 3, 1]
[1, 4, 6, 4, 1]
...etc.

(It’s a bit off due to the brackets/commas, but I’m just trying to get the general format down right now.)

Thank you for any help you can offer!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:42:48+00:00Added an answer on May 18, 2026 at 1:42 am
    h = int(raw_input("Please enter the height of the triangle: "))
    for i in mytri(h):
        print " " * (h * 2), i
        h -= 1
    

    So here you print 2 spaces for each level of pyramid. First line gets indented by twice the height number of spaces. As you descent one level, you decrease indentation by 2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on a homework assignment where I have to create a
I am currently working on a homework assignment and I am thoroughly stuck. I
I have a small python script I am working on for a class homework
I'm currently working on a homework assignment where I need to set up a
I'm working on a homework assignment for CS1, and I almost have it finished
The Background I am currently working on small MIPS program for a homework assignment,
I am working on a homework assignment for a class and I have solved
I'm working on a homework assignment for CS1, it's supposed to bring us back
This is homework...I'm not asking for answers, I just have a bug I'm not
G'day guys, Trying currently to finish up a bit of homework I'm working on,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.