Skip to main content
Became Hot Network Question
Tweeted twitter.com/StackCodeReview/status/1444814539359391750
improve title, add ascii-art tag
Source Link
ggorlen
  • 4k
  • 2
  • 18
  • 28

Can this python code for printing Printing a certain patterndiamond shape be improved?in ASCII

import math

num_rows = int(input())
if num_rows % 2 != 0:
    upper_mid = math.ceil(num_rows/2)
    lower_mid = num_rows//2
    
    #prints upper normal triangle
    for row in range(0, upper_mid):                 
        for col in range(upper_mid-1, row, -1):     
            print(end=" ")                         
        for col in range(row+1):                  
            print('*', end="")
        for col in range(row):                   
            print('*', end="")
        print()
    
    # prints lower inverted triangle 
    for row in range(0, lower_mid):                
        for col in range(row+1):                    
            print(end=" ")
        for col in range(lower_mid - row):         
            print('*', end="")
        for col in range(lower_mid - row-1):    
            print('*', end="")
        print()
else:
    print("Only odd numbers!")
import math

num_rows = int(input())
if num_rows % 2 != 0:
    upper_mid = math.ceil(num_rows/2)
    lower_mid = num_rows//2
    
    #prints upper normal triangle
    for row in range(0, upper_mid):                 
        for col in range(upper_mid-1, row, -1):     
            print(end=" ")                         
        for col in range(row+1):                  
            print('*', end="")
        for col in range(row):                   
            print('*', end="")
        print()
    
    # prints lower inverted triangle 
    for row in range(0, lower_mid):                
        for col in range(row+1):                    
            print(end=" ")
        for col in range(lower_mid - row):         
            print('*', end="")
        for col in range(lower_mid - row-1):    
            print('*', end="")
        print()
else:
    print("Only odd numbers!")

This code works 100%, and prints a diamond shape star pattern. I'm a beginner in pythonPython, and wrote this by myself, so I might have wrotewritten something unnecessary and the code could be simplified, or something else. Feedback is appreciated.

Can this python code for printing a certain pattern shape be improved?

import math

num_rows = int(input())
if num_rows % 2 != 0:
    upper_mid = math.ceil(num_rows/2)
    lower_mid = num_rows//2
    
    #prints upper normal triangle
    for row in range(0, upper_mid):                 
        for col in range(upper_mid-1, row, -1):     
            print(end=" ")                         
        for col in range(row+1):                  
            print('*', end="")
        for col in range(row):                   
            print('*', end="")
        print()
    
    # prints lower inverted triangle 
    for row in range(0, lower_mid):                
        for col in range(row+1):                    
            print(end=" ")
        for col in range(lower_mid - row):         
            print('*', end="")
        for col in range(lower_mid - row-1):    
            print('*', end="")
        print()
else:
    print("Only odd numbers!")

This code works 100%, and prints a diamond shape star pattern. I'm a beginner in python, and wrote this by myself, so I might have wrote something unnecessary and the code could be simplified, or something else. Feedback is appreciated.

Printing a diamond shape in ASCII

import math

num_rows = int(input())
if num_rows % 2 != 0:
    upper_mid = math.ceil(num_rows/2)
    lower_mid = num_rows//2
    
    #prints upper normal triangle
    for row in range(0, upper_mid):                 
        for col in range(upper_mid-1, row, -1):     
            print(end=" ")                         
        for col in range(row+1):                  
            print('*', end="")
        for col in range(row):                   
            print('*', end="")
        print()
    
    # prints lower inverted triangle 
    for row in range(0, lower_mid):                
        for col in range(row+1):                    
            print(end=" ")
        for col in range(lower_mid - row):         
            print('*', end="")
        for col in range(lower_mid - row-1):    
            print('*', end="")
        print()
else:
    print("Only odd numbers!")

This code works 100%, and prints a diamond shape star pattern. I'm a beginner in Python, and wrote this by myself, so I might have written something unnecessary and the code could be simplified, or something else. Feedback is appreciated.

added 124 characters in body
Source Link
Anony
  • 123
  • 5
import math

num_rows = int(input())
if num_rows % 2 != 0:
    upper_mid = math.ceil(num_rows/2)
    lower_mid = num_rows//2
    
    #prints upper normal triangle
    for row in range(0, upper_mid):                 
        for col in range(upper_mid-1, row, -1):     
            print(end=" ")                         
        for col in range(row+1):                  
            print('*', end="")
        for col in range(row):                   
            print('*', end="")
        print()
    
    # prints lower inverted triangle 
    for row in range(0, lower_mid):                
        for col in range(row+1):                    
            print(end=" ")
        for col in range(lower_mid - row):         
            print('*', end="")
        for col in range(lower_mid - row-1):    
            print('*', end="")
        print()
else:
    print("Only odd numbers!")

Input:

9

Ouput:

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

This code works 100%, and prints a diamond shape star pattern. I'm a beginner in python, and wrote this by myself, so I might have wrote something unnecessary, or and the code could be simplified, or something else. Feedback is appreciated.

import math

num_rows = int(input())
if num_rows % 2 != 0:
    upper_mid = math.ceil(num_rows/2)
    lower_mid = num_rows//2
    
    #prints upper normal triangle
    for row in range(0, upper_mid):                 
        for col in range(upper_mid-1, row, -1):     
            print(end=" ")                         
        for col in range(row+1):                  
            print('*', end="")
        for col in range(row):                   
            print('*', end="")
        print()
    
    # prints lower inverted triangle 
    for row in range(0, lower_mid):                
        for col in range(row+1):                    
            print(end=" ")
        for col in range(lower_mid - row):         
            print('*', end="")
        for col in range(lower_mid - row-1):    
            print('*', end="")
        print()
else:
    print("Only odd numbers!")

This code works 100%, and prints a diamond shape star pattern. I'm a beginner in python, and wrote this by myself so I might have wrote something unnecessary, or the code could be simplified. Feedback is appreciated.

import math

num_rows = int(input())
if num_rows % 2 != 0:
    upper_mid = math.ceil(num_rows/2)
    lower_mid = num_rows//2
    
    #prints upper normal triangle
    for row in range(0, upper_mid):                 
        for col in range(upper_mid-1, row, -1):     
            print(end=" ")                         
        for col in range(row+1):                  
            print('*', end="")
        for col in range(row):                   
            print('*', end="")
        print()
    
    # prints lower inverted triangle 
    for row in range(0, lower_mid):                
        for col in range(row+1):                    
            print(end=" ")
        for col in range(lower_mid - row):         
            print('*', end="")
        for col in range(lower_mid - row-1):    
            print('*', end="")
        print()
else:
    print("Only odd numbers!")

Input:

9

Ouput:

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

This code works 100%, and prints a diamond shape star pattern. I'm a beginner in python, and wrote this by myself, so I might have wrote something unnecessary and the code could be simplified, or something else. Feedback is appreciated.

Source Link
Anony
  • 123
  • 5

Can this python code for printing a certain pattern shape be improved?

import math

num_rows = int(input())
if num_rows % 2 != 0:
    upper_mid = math.ceil(num_rows/2)
    lower_mid = num_rows//2
    
    #prints upper normal triangle
    for row in range(0, upper_mid):                 
        for col in range(upper_mid-1, row, -1):     
            print(end=" ")                         
        for col in range(row+1):                  
            print('*', end="")
        for col in range(row):                   
            print('*', end="")
        print()
    
    # prints lower inverted triangle 
    for row in range(0, lower_mid):                
        for col in range(row+1):                    
            print(end=" ")
        for col in range(lower_mid - row):         
            print('*', end="")
        for col in range(lower_mid - row-1):    
            print('*', end="")
        print()
else:
    print("Only odd numbers!")

This code works 100%, and prints a diamond shape star pattern. I'm a beginner in python, and wrote this by myself so I might have wrote something unnecessary, or the code could be simplified. Feedback is appreciated.