+ 3
Generate random number then make triangle pattern in python.
How can I generate a random number between 5 and 10 then print that random number and make a triangle pattern counting down to 1 on each line. Example: The random number generated is 5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1
3 Antworten
+ 1
Thanks for the answers,was looking for this :
def inverted_tree(length):
for length_2 in range(length, 0, -1):
for k in range(length_2, 0, -1):
print(k, end=' ')
print()
return ''
from random import choice
k = choice(range(5,10))
print("The random number generated is",k)
print(inverted_tree(k))
- 1
I don't know python but i can tell you how is it going to be done in java(Logic is same).
1:Use random method from "math" class.
2:Give that number into a method that does the work of creating triangle like following(Compiled and runnable):
public void generateTriangle(int random_number){
for(int i=random_number;i>=1;i--){
for(int k=i;k>=1;k--){
System.out.print(k+" ");
}
System.out.println("");
}
}
3:Call the "generateTriangle" method and ...Ta..Da :P
By the way i don't like pattern challenges because they are all done using "nested loops" and its kinda useless in my opinion for any real world task.