Saturday, 31 August 2013

Python Logic - decreasing set of 1's using for loops to create triangle

Python Logic - decreasing set of 1's using for loops to create triangle

This is for a CS101 type Python class.
The problem asks the programmer to write for loops which will set up this
pattern:
111
11
1
I have example code here which produces
11111
11111
11111
11111
11111
for i in range(0, 5):
X = 0
for j in range(0, 5):
X = (X*10)+1
print(X)
Here's one of my attempts. Obviously it doesn't work. Please help!
n=int(input())
for i in range(0, n):
X = 0
for j in range(0, n):
X = (X*10)+1
i=(n-2)
print(X)
Please try to explain an answer using loops only. We haven't gotten to
functions. I have this stackoverflow answer but it is written as a
function.

No comments:

Post a Comment