The user enters a number and the program should output that amount of rows and colunmns. I have tried a couple of different ways butcant quite get it even though I think I am close. for example if 10 is entered it should look something like this:

O

OO

OOO

OOOO



OOOO

OOO

OO

O

Code:

num = 0
#user input
num = int(raw_input('Give me a number up to 10: '))    

for a in range(0,num +1,1):
    print "*" * a
#make a space for the next rows that count down
print
for a in range(num,0,-1):print'*' * a
print'done'
#a different way
num = 0
num = int(raw_input('What is your number: '))
for row in range(1, num + 1, 1):
    for column in range(1, num, 2):
        circleList = list()
        circleList.append(row)
        print'*'

Dani AI

Generated

A quick clarification for and others: the example shown (for input 10) is a triangle whose row-lengths sum to 10 (1+2+3+4 = 10), then the same triangle reversed. The original loops print a fixed count of rows equal to the input instead. The clean way to get the triangle in the example is to pick the largest k with 1 + 2 + ... + k <= N (triangular numbers), then handle any leftover remainder.

Compute k from the quadratic formula (k(k+1)/2 <= N). If remainder = N - k(k+1)/2 is nonzero, append one extra row of that many characters. Build a list of row lengths, print them, print a blank line, then print them in reverse. Example implementation (Python 3):

import math

n = int(input("Enter number: "))   # use raw_input() in Python 2
k = int((math.sqrt(8*n + 1) - 1) / 2)
used = k*(k+1)//2
rem = n - used

rows = list(range(1, k+1))
if rem:
    rows.append(rem)

for r in rows:
    print('O' * r)
print()
for r in reversed(rows):
    print('O' * r)

Notes and troubleshooting: 's counter approach is a good intuition (count printed objects until reaching N), but the formula above gets k in O(1) time and avoids off-by-one mistakes. Be careful with integer division and ranges (use range(1, k+1) for 1..k). For Python 2 replace input() with raw_input() or use from __future__ import print_function. Decide how to handle small or negative N (treat as no output or validate input). If an exact triangular number is required, check rem == 0 and handle non-triangular inputs according to the desired behavior (append remainder row as above or redistribute it).

Recommended Answers

All 4 Replies

num = 0
#user input
num = int(raw_input('Give me a number up to 10: '))    

for a in range(0,num +1,1):
    print "*" * a
#make a space for the next rows that count down
print
for a in range(num,0,-1):print'*' * a
print'done'
#a different way
num = 0
num = int(raw_input('What is your number: '))
for row in range(1, num + 1, 1):
    for column in range(1, num, 2):
        circleList = list()
        circleList.append(row)
        print'*'

circlelist is never used and it is list(range(1, num + 1)) why you produce the numberlist?

The user enters a number and the program should output that amount of rows and colunmns.

Your for() loop actually prints the same number of __rows__ as the number entered by the user. Instead, you want to use a counter to count each object when it is printed/displayed. According to your diagram, you want to print up to number/2 and then print the bottom half=number/2 as well. Also, how would you print if an odd number, like 9, was entered. Here's an example for the top half to get you started:

number = 12
ctr = 0
row = 1

while ctr < number/2:
    print "*" * row, 
    ctr += row     ## number of objects printed
    print "    row =", row, "ctr is now", ctr
    row += 1

Ok I see whet you are talking about now.
Thanks Again,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.