Vincent Iyegbuye
Vincent Iyegbuye

Reputation: 11

errors in my code that involve diamond pattern,

Write a program that prompts the user to enter the number of diamonds they want to draw and then, on the next line, takes a series of integers representing the edge lengths of each diamond. The program should then output a visual representation of nested diamonds using asterisk (*) characters. Each diamond should be drawn inside the previous one, and the size of each diamond is determined by the corresponding input.

    def generate_nested_diamonds(diamond_sizes):
        nested_diamonds = []
    
        # Find the maximum edge length to determine the size of the outermost diamond
        max_edge_length = max(diamond_sizes)
        outermost_diamond_size = 2 * max_edge_length - 1
    
        # Find the center of the outermost diamond
        center_row = outermost_diamond_size // 2
        center_col = outermost_diamond_size // 2
    
        for edge_length in diamond_sizes:
            p = edge_length
            n = 2 * p - 1
    
            # Create a 2D list to represent the diamond pattern
            diamond_pattern = [[' ' for _ in range(outermost_diamond_size)] for _ in range(outermost_diamond_size)]
    
            # Find the starting point for the current diamond inside the outermost diamond
            start_row = center_row - n // 2
            start_col = center_col - n // 2
    
            # Fill in the diamond pattern in the 2D list
            for row_index in range(n):
                for col_index in range(n):
                    if row_index + col_index == n // 2 or col_index - row_index == n // 2 or row_index - col_index == n // 2 or row_index + col_index == (
                            n - 1) + n // 2:
                        diamond_pattern[start_row + row_index][start_col + col_index] = '*'
    
            nested_diamonds.append(diamond_pattern)
    
        return nested_diamonds
    
    
    def print_nested_diamonds(nested_diamonds):
        max_size = len(nested_diamonds[0])
    
        for diamond in nested_diamonds:
            for row in diamond:
                print(' '.join(row).center(max_size))
    # Get user input for the edge lengths of each diamond
    user_diamond_sizes = []
    num_diamonds = int(input("Enter the number of diamonds: "))
    for diamond_index in range(num_diamonds):
        try:
            user_edge_length = int(input(f"Enter the edge length of diamond {diamond_index + 1}: "))
            user_diamond_sizes.append(user_edge_length)
        except ValueError:
            print("Edge length must be an integer.")
            exit()
    
    # Generate nested diamonds using a 2D list
    user_nested_diamonds = generate_nested_diamonds(user_diamond_sizes)
    
    # Print the nested diamonds
    print_nested_diamonds(user_nested_diamonds)

I EXPECTED

1
3

Output:

  *
 * *
*   *
 * *
  *

Input example 3 (3 diamonds of edge lengths 6, 3, and 1):

3
6 3 1

Output:

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

Input example 3 (3 diamonds of edge lengths 3, 2, and 1):

3
3 2 1

Output:

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

`MY RESULTS:

Enter the number of diamonds: 3
Enter the edge length of diamond 1: 6
Enter the edge length of diamond 2: 3
Enter the edge length of diamond 3: 1
          *          
        *   *        
      *       *      
    *           *    
  *               *  
*                   *
  *               *  
    *           *    
      *       *      
        *   *        
          *          

                     
                     
                     
          *          
        *   *        
      *       *      
        *   *        
          *          
                     
                     
                     

                     
                     
                     
                     
                     
          *          
                

Upvotes: 1

Views: 26

Answers (0)

Related Questions