Hướng dẫn inverted solid right triangle in python using for loop - đảo ngược tam giác vuông rắn trong python bằng vòng lặp for

Trong bức ảnh này, chúng ta sẽ thảo luận về cách tạo ra một tam giác góc phải ngược bằng cách sử dụng các số trong Python.

Chúng ta có thể in rất nhiều mẫu bằng Python. Điều kiện tiên quyết duy nhất để làm điều này là một sự hiểu biết tốt về cách các vòng lặp hoạt động trong Python.

Ở đây, chúng tôi sẽ sử dụng các vòng for đơn giản để tạo hình tam giác góc phải ngược bằng số.

Sự mô tả

Một tam giác được cho là góc phải nếu nó có một góc bằng 90 độ ở phía bên trái của nó. Một hình tam giác góc phải đảo ngược chỉ là dạng đảo ngược của điều này với đỉnh của nó nằm ở phía dưới.right-angled if it has an angle equal to 90 degrees on its left side. An inverted right-angled triangle is just the inverted form of this with its vertex lying on the bottom.

Để thực hiện điều này bằng cách sử dụng lập trình Python, chúng tôi sẽ sử dụng hai vòng for:

  • Một vòng ngoài bên ngoài: Để xử lý số lượng hàng.: To handle the number of rows.
  • Một vòng lặp bên trong: Để xử lý số lượng cột.: To handle the number of columns.

Mã số

Hãy cùng nhìn vào đoạn mã dưới đây.

# Number of rows

rows = 5

# Loop over number of rows

for i in range[rows+1, 0, -1]:

# Nested reverse loop to handle number of columns

for j in range[0, i-1]:

# Display pattern

print[j+1, end=' ']

print[" "]

Giải trình

  • Trong dòng 2, đầu vào cho số lượng hàng [nghĩa là chiều dài của tam giác] được thực hiện.

  • Trong dòng 5, chúng tôi tạo một vòng for để xử lý số lượng hàng. Vòng lặp là một vòng đảo ngược, tức là, nó bắt đầu với giá trị đầu vào và với các hàng tăng, số lượng ký tự được in giảm.

  • Trong dòng 8, chúng tôi tạo một vòng lặp for lồng nhau [vòng bên trong], để xử lý số lượng cột. Điều này tuân theo nguyên tắc tương tự như trên, giúp tạo ra một hình tam giác đảo ngược.

  • Trong dòng 11, chúng tôi đã in j+1, dẫn đến việc lặp lại từ 1 [vì J + 1] thành chiều dài [hàng-i] trong mỗi hàng. Khi

    Inverted Solid Right Triangle
    
    Given an integer number 
    
    N as input. Write a program to print the right-angled triangular pattern of N lines as shown below.
    
    Note: There is a space after each asterisk [*] character.
    
    Input
    
    The first line of input is an integer 
    
    N.
    
    Explanation
    
    In the given example the solid right angled triangle of side 
    
    4. Therefore, the output should be
    
    * * * * 
      * * * 
        * * 
          *
    Sample Input 1
    4
    Sample Output 1
    * * * * 
      * * * 
        * * 
          *
    Sample Input 2
    5
    Sample Output 2
    * * * * * 
      * * * * 
        * * * 
          * * 
            *
    
    
    0 tiếp tục tăng sau mỗi lần lặp, số lượng số nguyên tiếp tục giảm.

  • Trong dòng 12, chúng tôi sử dụng

    Inverted Solid Right Triangle
    
    Given an integer number 
    
    N as input. Write a program to print the right-angled triangular pattern of N lines as shown below.
    
    Note: There is a space after each asterisk [*] character.
    
    Input
    
    The first line of input is an integer 
    
    N.
    
    Explanation
    
    In the given example the solid right angled triangle of side 
    
    4. Therefore, the output should be
    
    * * * * 
      * * * 
        * * 
          *
    Sample Input 1
    4
    Sample Output 1
    * * * * 
      * * * 
        * * 
          *
    Sample Input 2
    5
    Sample Output 2
    * * * * * 
      * * * * 
        * * * 
          * * 
            *
    
    
    1, để chuyển sang dòng tiếp theo.

Người đóng góp

Vinisha Maheshwari

Inverted Solid Right Triangle

This Program name is Inverted Solid Right Triangle. Write a Python program to Inverted Solid Right Triangle, it has two test cases

The below link contains Inverted Solid Right Triangle question, explanation and test cases

//drive.google.com/file/d/1YFU7WKt1VLzQJ6HEmvwToqs3_quADbZ8/view?usp=sharing

We need exact output when the code was run

Inverted Solid Right Triangle

Given an integer number 

N as input. Write a program to print the right-angled triangular pattern of N lines as shown below.

Note: There is a space after each asterisk [*] character.

Input

The first line of input is an integer 

N.

Explanation

In the given example the solid right angled triangle of side 

4. Therefore, the output should be

* * * * 
  * * * 
    * * 
      *
Sample Input 1
4
Sample Output 1
* * * * 
  * * * 
    * * 
      *
Sample Input 2
5
Sample Output 2
* * * * * 
  * * * * 
    * * * 
      * * 
        *

def invertedTriangle[rows]:
 
    i = rows
    while i >= 1:
        j = rows
        while j > i:
           
            print[' ', end=' ']
            j -= 1
        k = 1
        while k 

Bài Viết Liên Quan

Chủ Đề