Python inverted number pyramid exercise - python

This is an example of a correct pyramid.
I have this exercise as homework but I cant figure it out.
The code i've tried so far is this one and I can't make it inverted:
n = 5
for i in range(n, 0, -1):
for j in range(n, i-1, -1):
print(j, end=" ")
print(" ")

n = int(input("What's the number: "))
for i in range(n, 0, -1):
for j in range(i, 0, -1):
print(j, end="")
print()
Works for me.
The trick is to start the second loop "j" with the decrementing "i" downwards.
Imagine you are doing not a pyramid, but printing
5 4 3 2 1 n times.
Something like
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
...
You would start i and j at the same index, n, and just repeat the task.
Here, you start j at i, so each time i gets decremented, j starts from the decremented i, thus making a pyramid effect.
Hope you understand :)

Related

for loop in pyton exercise

I'm looking for help to solve the below problem: Having 2 variables beat and measures I need to create a loop that prints, on the same line, all the beats times the number of measures. Nevertheless, every time it starts a new measure, the first beat should be equal to the number of the current measure.
Example: beats_per_measure = 4, measures = 4 should give
1 2 3 4 2 2 3 4 3 2 3 4 4 2 3 4
and not
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
beat = beats_per_measure + 1
measure = measures + 1
for i in range(1,measure):
for j in range(1, beat):
print(j, end = " ")
This is the code I've tried to print beats x measure, but I'm not sure how to change that first beat to reflect the current measure.
This code gave:123123123, maybe you lost "space" somewhere.
for i in range(1,4):
for j in range(1, 4):
print(j, end = "")
for i in range (1, measure+1):
for j in range(1, beat+1):
if j==1:
print(i, end="")
else:
print(j, end="")
Don't overthink a problem like this; adding an extra print provides the expected output, no if required:
measures = 4
beats = 4
for measure in range(1, measures+1):
print(measure, end="")
# Start beat from 2
for beat in range(2, beats+1):
print(beat, end="")

Python How to make a number triangle

So I've recently started learning python and my friend gave me a changllege. How to make a number triangle. So basically, I need to make python let me input a number, for example 5. After I do that, I want python to print
54321
4321
321
21
1
But because I am new to python, I don't know how to do it.
So far, I've got this:
x = int(input('Enter a Number: '))
for i in range(x,0,-1):
print(i,end='')
if i != 0:
continue
else:
break
And the output is:
Enter a Number: 5
54321
Any ideas how to make it print
54321
4321
321
21
1
?
Thanks for any advice
Here is a sample code for you.
Short version (suggest learning about list comprehension and join functions):
x = int(input('Enter a Number: '))
for i in range(x, 0, -1):
print(''.join([str(j) for j in range(i, 0, -1)]))
Longer version which is easier to understand:
for i in range(x, 0, -1):
s = ''
for j in range(i, 0, -1):
s += str(j)
print(s)
Output:
54321
4321
321
21
1
rows = int(input("Inverted Right Triangle Numbers = "))
print("Inverted")
for i in range(rows, 0, -1):
for j in range(i, 0, -1):
print(j, end = ' ')
print()
Output
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
def trig_print(number):
if number > 0:
for i in range(number):
print(number - i,end='')
print('')
number = number - 1
trig_print(number)
else:
return 0
trig_print(5)
I used to solve it by while loop.
3 step
input initial number.
make list one to initial number.
reverse list and print it.
>>> x = int(input('Enter a Number: '))
Enter a Number: 5
>>> while x >0 :
l=list(range(1,x+1)) # range function
l.reverse() # list.reverse function
print(' '.join(map(str,l))) # join and map function
# I recomanded input space between number for readability.
x -= 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1

How do I modify this code so that it will print the pattern correctly?

for i in range(2):
for j in range(1,11):
print(j," ",end="")
print()
I need to print this pattern, but I can't figure how to get it to subtract 1 on the next line. Please help.
1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9
Is this what you meant?
for i in range(2):
for j in range(1, 11):
print(j - i, end=" ")
print()
Another way, just for fun :) would be limiting the range of j with current value of i:
>>> for i in range(1, -1, -1):
for j in range(i, 10+i):
print (j, end=" ")
print()
1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9
OR
>>> for i in range(2):
for j in range(1-i, 11-i):
print (j, end=" ")
print()
Using list comprehension you can do like this also: This is different approach.
print ("\n".join([" ".join([str(j) for j in range(1,11)])] + [" ".join([str(i-1) for i in range(1,11)])]))
for i in range(2):
for j in range(1,11):
print(j - i," ",end="")
if you want results in one line try this

Number half Pyramid Python

I'm trying to print a half pyramid that stars on the left side in python.
So far, this is my code
for i in range(1,12):
for j in range(12 - i):
print(" ", end = " ")
for j in range(1, i):
print(j, end = " " )
print("\n")
and my output is
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
However, my output is meant to be in the opposite order:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
How can I make this change?
Just reverse the second loop -- the one that prints that actual numbers:
for j in range(i-1, 0, -1):
The last parameter controls the "step", or how much the variable changes on each loop iteration. Output:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
...
Reverse the range by adding the third argument (-1). Also format your numbers to use 2 places, so 10 is not pushing the last line to the right. Finally, the last print should probably not have \n, since that is already the default ending character of print:
for i in range(1,12):
for j in range(12 - i):
print(" ", end = "")
for j in range(i-1, 0,-1):
print(str(j).rjust(2), end = "" )
print()
You could just reverse the range that you print out as numbers
for i in range(1,12):
for j in range(12 - i):
print(" ", end = " ")
for j in reversed(range(1, i)):
print(j, end = " " )
print("\n")
The problem is in your second for loop, as you are looping from 1 to i, meaning you start off with 1 being printed first, and every following number until (not including) i.
Fortunately, for loops are able to go in reverse. So, instead of:
for j in range(1, i)
You could write:
for j in range((i-1), 0, -1)
Where the first parameter is signifies where the loop starts, the second is where the loop finishes, and the third signifies how large our jumps are going to be, in this case negative. The reason we are starting at i-1 and finishing at 0 is because loops start at exactly the first given number, and loop until just before the second given number, so in your given code the loop stops just before i, and so this one starts just before i as well, although you could remove the -1 if you wish to include 12 in the pyramid.

For loop a square pattern of numbers

In this question I had to create a program that prompts the user for a number and then prompt again for how many rows to create. Something like:
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3
4 4 4 4 4 4 4
This is what I came up with and I have tried many different ways to get the same result but it didn't work.
num=int(input("Enter a number between 1 and 10: "))
rows=int(input("Enter how many rows to of numbers: "))
for i in range(num):
print(i,end=" ")
for x in range(rows):
print (x)
This is the output I came up with:
Enter a number between 1 and 10: 6
Enter how many rows to of numbers: 4
0 1 2 3 4 5 0
1
2
3
You may simply do it like:
num = 5
rows = 4
for i in range(1, num+1):
print('{} '.format(i) * rows)
Output:
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
Explanation: When you multiply a str with some number say n, new string is returned with original string repeated n times. Doing this you will eliminate your nested loop
Simple solution: Just use nested for loops:
num = int(input("Enter a number between 1 and 10: "))
rows = int(input("Enter how many rows to of numbers: "))
for i in range(num):
print ('\n')
for x in range(rows):
print (i + 1)
The code above will go through the range 0 to num, printing first a new line and then printing the current number rows times.
rows = 5
side = int(input("Please Enter any Side of a Square : "))
for i in range(side):
for j in range(side):
if(i == 0 or i == side - 1 or j == 0 or j == side - 1):
print(i, end = ' ')
else:
print(i, end = ' ')
print()

Categories

Resources