Solution

Let's break the problem down into steps.

  1. First, we need to get an integer from the input, and make sure that the integer is either 3, 10, or any number in between. That's what 3 to 10 inclusive means.

  2. We then need to ask a user for a single character that will be used to print a box.

  3. After we've gotten the integer, and the character, we need to display a box made up of the character whose width and height are determined by the integer.

For step one, we need to get input from the user. This can be done using the builtin function input(). It takes an optionally prompt as an argument, and returns the input as a string. So let's write that down:

integer = input()

Great. Now we need to convert the input to an integer, so it can be used in the rest of our program. This can be accomplished using the built-in function int(). We'll pass in your string, and it will convert it to an integer:

integer = int(input('Enter a number: '))

Step two is to get a character from the user. That's easy. We'll use input() once again to get a single character from the user. but since the input need to be a character, we shouldn't convert this value:

character = input('Enter a character: ')

Alright. This is what we have so far:

integer = int(input('Enter a number: '))
character = input('Enter a character: ')

The last step is to print a box made of the value of character that has a width of integer and a height of integer. Now all we need to do is to print a row with integer number of characters integer number of times. This is where the nested for loop comes in.

We know each row must be the width of integer. So we need to print character that many times. We can use the range() built-in function to do this. We pass in integer as an argument, and it will generate all of the numbers from 0 to integer:

for _ in range(integer):
    print(character, end="")

The the reason we used the underscore in our for loop instead of an actual readable variable name, is because we don't need to use the values that the for loops would give us. We only care about repeating our code.

The end="" part is telling the print() builtin function not to print a newline each time it is called, because we want all the character to be in one row. The print() before the for loop is to add some space between our prompts and the program output. Let's see what our program outputs so far:

Enter a number: 5
Enter a character: @

@@@@@
>>> 

Great. The last thing to do is to print each row integer number of times. Since we know that executing the for loop above prints one row, we just need to execute it integer number of times to print integer rows. So we'll wrap that for loop inside of another for loop and use range() to execute the nested for loop integer number of times. We'll use print() once again, to add a newline between each row:

print()
for _ in range(integer):
    for _ in range(integer):
        print(character, end="")
    print()

And that's it! Were done. Let's test our program:

Enter a number: 7
Enter a character: #

#######
#######
#######
#######
#######
#######
#######
>>> 

As you can see, we're getting our expected output. Here is the final program:

integer = int(input('Enter a number: '))
character = input('Enter a character: ')

print()
for _ in range(integer):
    for _ in range(integer):
        print(character, end="")
    print()

Error handling

You may also need to implement error handling. This means making your program recover from the user entering invalid input. In this case, the only time invalid input could really be enetered is when we are asking the user for an integer. We need to:

  1. make sure the user enetered a valid number. And
  2. make sure that number is either 3, 10 or any number in between.

To do this, we'll need a loop. This is so we can continue to ask the user for valid input, until they do so. We can use a while True loop. This means we'll keep looping until we break from the loop.

while True:

Now we need to make sure the user entered a valid number. We can use a try/except block. The way a try/except block works is that we put a block of code in the try portation that may raise an error. if no error is raised, we continue in the program normally. If an error is raised however, Python will jump to the except block and the code in there will be executed.

In our case we'll try to convert the users input to an integer. If this fails a ValueError will be raised. So we need to catch that error message and print it to the user:

while True:
    try:
        integer = int(input())
    except ValueError:
        print('Please enter a number')

All that's left is to test if the integer entered is in the correct range. If so, we'll break from our loop and continue with the program. If not, we'll display an error message and try again. Here is how that would look:

while True:
    try:
        integer = int(input('Enter a number: '))
        if integer >= 3 and integer <= 10:
            break
        print('Please enter a number between 3 and 10 inclusive')
    except ValueError:
        print('Please enter a number')

character = input('Enter a character: ')

print()
for _ in range(integer):
    for _ in range(integer):
        print(character, end="")
    print()

Here is how the above would look in the full program:

And here is a demo:

Enter a number: -23
Please enter a number between 3 and 10 inclusive
Enter a number: a
Please enter a number
Enter a number: 11
Please enter a number between 3 and 10 inclusive
Enter a number: 10
Enter a character: #

##########
##########
##########
##########
##########
##########
##########
##########
##########
##########
>>> 

For more resources about error handling in a program, see the question: Asking the user for input until they give a valid response.

Answer from Chris on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-nested-loops
Python Nested Loops - GeeksforGeeks
In Python, there are two types of loops: for loop and while loop. Using these loops, we can create nested loops, which means loops inside a loop. For example, a while loop inside a for loop, or a for loop inside another for loop.
Published   March 13, 2026
🌐
PYnative
pynative.com › home › python › nested loops in python
Python Nested Loops [With Examples] – PYnative
September 2, 2021 - A nested loop is a part of a control flow statement that helps you to understand the basics of Python. In Python, the for loop is used to iterate over a sequence such as a list, string, tuple, other iterable objects such as range.
Discussions

python - What is a nested loop and how do I use it in example below? - Stack Overflow
Write a program in Python to prompt user to enter an integer between 10 and 3 inclusive and also a character. Then use these two values to display a box using the character entered with size of box determined by the number entered. ... Your program will run only once but you must used repetition construct to draw the box using values entered. Hint: It should be a nested loop... More on stackoverflow.com
🌐 stackoverflow.com
[Python] Can someone please explain to me how nested for loops work using this example. I don't understand how it works
What does this do? for j in range(i,userInput): print('*', end=' ') If you don't know, try it. Assign values to i and userInput manually and see if you can figure it out. More on reddit.com
🌐 r/learnprogramming
15
1
March 11, 2025
What is the best way to vectorize the below for loop for computational efficiency?

So, this is not answering the question (others have already done so), but one option for something like that would be @numba. It's potentially faster than vectorization in this case by avoiding allocating intermediate values.

It's also a potential one liner change.

import numba as nb
import numpy as np

def f(I, xneighb, yneighb):
O = np.zeros_like(I)
xdir, ydir = I.shape
xdir -= xneighb
ydir -= yneighb
for i in range(xdir):
for j in range(ydir):
for m in range(xneighb):
for l in range(yneighb):
if I[i,j]>I[i+m,j+l]:
O[i,j]=O[i,j]+1
return O

f_c = nb.jit(f)

input = np.random.rand(800,800)

f_c runs in 15ms, f in 3s

More on reddit.com
🌐 r/Python
11
13
November 9, 2017
Converting N nested loops into recursive function when dependent on loop indexing?

Pass it as an argument to the function.

More on reddit.com
🌐 r/learnprogramming
2
2
January 30, 2016
People also ask

How does a Python nested while loop work?
In a nested while loop, one while loop runs inside another. You can use it to repeat tasks in layers, but you must be careful with conditions to avoid infinite loops.
🌐
wscubetech.com
wscubetech.com › resources › python › nested-loops
Nested Loops in Python: Uses, Working, Syntax, Examples
When should I use nested loops in Python?
Use Python nested loops when solving problems involving rows and columns, combinations, or patterns. It helps when one action depends on another being repeated.
🌐
wscubetech.com
wscubetech.com › resources › python › nested-loops
Nested Loops in Python: Uses, Working, Syntax, Examples
Can nested loops be used to print patterns in Python?
Python nested loops are perfect for creating number, star, or triangle patterns. The outer loop handles rows, and the inner loop controls what’s printed in each row.
🌐
wscubetech.com
wscubetech.com › resources › python › nested-loops
Nested Loops in Python: Uses, Working, Syntax, Examples
🌐
W3Schools
w3schools.com › python › gloss_python_for_nested.asp
Python Nested Loops
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... A nested loop is a loop inside a loop.
🌐
Real Python
realpython.com › nested-loops-python
Nested Loops in Python – Real Python
February 22, 2025 - Take the Quiz: Test your knowledge with our interactive “Nested Loops in Python” quiz. You’ll receive a score upon completion to help you track your learning progress: ... Nested loops allow you to perform repeated actions over multiple sequences, but is there more?
🌐
OpenStax
openstax.org › books › introduction-python-programming › pages › 5-3-nested-loops
5.3 Nested loops - Introduction to Python Programming | OpenStax
March 13, 2024 - A program to print available appointments can use a nested for loop where the outer loop iterates over the hours, and the inner loop iterates over the minutes. This example prints time in hours and minutes in the range between 8:00am and 10:00am.
🌐
Tutorialspoint
tutorialspoint.com › python › python_nested_loops.htm
Python - Nested Loops
In Python, when you write one or more loops within a loop statement that is known as a nested loop. The main loop is considered as outer loop and loop(s) inside the outer loop are known as inner loops.
Find elsewhere
🌐
Mimo
mimo.org › glossary › python › nested-loops
Python Nested Loops: Syntax, Usage, and Examples
A nested loop is simply a loop inside another loop. Python lets you combine any loop types, but the most common pattern is one for loop inside another for loop. ... Become a Python developer. Master Python from basics to advanced topics, including data structures, functions, classes, and error ...
Top answer
1 of 3
1

Solution

Let's break the problem down into steps.

  1. First, we need to get an integer from the input, and make sure that the integer is either 3, 10, or any number in between. That's what 3 to 10 inclusive means.

  2. We then need to ask a user for a single character that will be used to print a box.

  3. After we've gotten the integer, and the character, we need to display a box made up of the character whose width and height are determined by the integer.

For step one, we need to get input from the user. This can be done using the builtin function input(). It takes an optionally prompt as an argument, and returns the input as a string. So let's write that down:

integer = input()

Great. Now we need to convert the input to an integer, so it can be used in the rest of our program. This can be accomplished using the built-in function int(). We'll pass in your string, and it will convert it to an integer:

integer = int(input('Enter a number: '))

Step two is to get a character from the user. That's easy. We'll use input() once again to get a single character from the user. but since the input need to be a character, we shouldn't convert this value:

character = input('Enter a character: ')

Alright. This is what we have so far:

integer = int(input('Enter a number: '))
character = input('Enter a character: ')

The last step is to print a box made of the value of character that has a width of integer and a height of integer. Now all we need to do is to print a row with integer number of characters integer number of times. This is where the nested for loop comes in.

We know each row must be the width of integer. So we need to print character that many times. We can use the range() built-in function to do this. We pass in integer as an argument, and it will generate all of the numbers from 0 to integer:

for _ in range(integer):
    print(character, end="")

The the reason we used the underscore in our for loop instead of an actual readable variable name, is because we don't need to use the values that the for loops would give us. We only care about repeating our code.

The end="" part is telling the print() builtin function not to print a newline each time it is called, because we want all the character to be in one row. The print() before the for loop is to add some space between our prompts and the program output. Let's see what our program outputs so far:

Enter a number: 5
Enter a character: @

@@@@@
>>> 

Great. The last thing to do is to print each row integer number of times. Since we know that executing the for loop above prints one row, we just need to execute it integer number of times to print integer rows. So we'll wrap that for loop inside of another for loop and use range() to execute the nested for loop integer number of times. We'll use print() once again, to add a newline between each row:

print()
for _ in range(integer):
    for _ in range(integer):
        print(character, end="")
    print()

And that's it! Were done. Let's test our program:

Enter a number: 7
Enter a character: #

#######
#######
#######
#######
#######
#######
#######
>>> 

As you can see, we're getting our expected output. Here is the final program:

integer = int(input('Enter a number: '))
character = input('Enter a character: ')

print()
for _ in range(integer):
    for _ in range(integer):
        print(character, end="")
    print()

Error handling

You may also need to implement error handling. This means making your program recover from the user entering invalid input. In this case, the only time invalid input could really be enetered is when we are asking the user for an integer. We need to:

  1. make sure the user enetered a valid number. And
  2. make sure that number is either 3, 10 or any number in between.

To do this, we'll need a loop. This is so we can continue to ask the user for valid input, until they do so. We can use a while True loop. This means we'll keep looping until we break from the loop.

while True:

Now we need to make sure the user entered a valid number. We can use a try/except block. The way a try/except block works is that we put a block of code in the try portation that may raise an error. if no error is raised, we continue in the program normally. If an error is raised however, Python will jump to the except block and the code in there will be executed.

In our case we'll try to convert the users input to an integer. If this fails a ValueError will be raised. So we need to catch that error message and print it to the user:

while True:
    try:
        integer = int(input())
    except ValueError:
        print('Please enter a number')

All that's left is to test if the integer entered is in the correct range. If so, we'll break from our loop and continue with the program. If not, we'll display an error message and try again. Here is how that would look:

while True:
    try:
        integer = int(input('Enter a number: '))
        if integer >= 3 and integer <= 10:
            break
        print('Please enter a number between 3 and 10 inclusive')
    except ValueError:
        print('Please enter a number')

character = input('Enter a character: ')

print()
for _ in range(integer):
    for _ in range(integer):
        print(character, end="")
    print()

Here is how the above would look in the full program:

And here is a demo:

Enter a number: -23
Please enter a number between 3 and 10 inclusive
Enter a number: a
Please enter a number
Enter a number: 11
Please enter a number between 3 and 10 inclusive
Enter a number: 10
Enter a character: #

##########
##########
##########
##########
##########
##########
##########
##########
##########
##########
>>> 

For more resources about error handling in a program, see the question: Asking the user for input until they give a valid response.

2 of 3
0

A nested loop is a loop within a loop.

But how does it work?

The outer loop statement gets executed, which triggers the inner statement. The inner loop then executes till its completed..

So the inner statement will keep on going until it has satisfied the statement you have set it to.

Then, the outer loop triggers the inner loop again...and it will keep on triggering the inner loop forever until the outer loop's statement is satisfied.

🌐
WsCube Tech
wscubetech.com › resources › python › nested-loops
Nested Loops in Python: Uses, Working, Syntax, Examples
November 5, 2025 - Understand how Python nested loops work, their syntax, and practical examples to enhance your programming skills and solve complex problems efficiently.
🌐
Toppr
toppr.com › guides › computer-science › introduction-to-python › conditional-constructs-and-looping › nested-loops
Nested Loops: Python Nested Loops, Nested for Loop Syntax, FAQs
May 21, 2021 - Answer 1: A nested loop refers to a loop within a loop, an inner loop within the body of an outer one. Further, the first pass of the outer loop will trigger the inner loop, which will execute to completion.
🌐
Python Basics
pythonbasics.org › home › python basics › nested loops
Nested loops - pythonbasics.org
A loop can contain one or more other loops: you can create a loop inside a loop. This principle is known as nested loops. Nested loops go over two or more loops
🌐
Softuni
python-book.softuni.org › chapter-06-nested-loops.html
Chapter 6.1. Nested Loops - Programming Basics with Python
Here is an example that illustrates nested loops. The aim is again to print a rectangle made of N x N asterisk, in which for each row a loop iterates from 1 to N, and for each column a nested loop is executed from 1 to N: In Python, when the standard initial value of the variable in the loop (i = 0) does not work for us, we can change it with the above syntax.
🌐
Study.com
study.com › computer science courses › computer science 113: programming in python
Nested Loops in Python: Definition & Examples - Lesson | Study.com
July 12, 2019 - We also learned that nesting a loop means simply having a loop that has inside its commands another loop. Examples of the break and continue commands employed both in the inner and outer loops were detailed with the resulting outputs and their ...
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › nested for loop in python
Nested For Loop in Python | Logic, Syntax, and Examples
March 31, 2026 - You should use nested loops in Python when you need to iterate over items within items, such as rows and columns in a table, elements in a matrix, or characters in a list of strings.
🌐
Codingem
codingem.com › home › nested loops in python: a complete guide
Nested Loops in Python: A Complete Guide - codingem.com
January 25, 2024 - A nested loop in Python is a loop inside a loop. This guide teaches you how to work with nested loops in Python with illustrative examples.
🌐
InterServer
interserver.net › home › python › everything you need to know about python nested loops
Everything You Need to Know About Python Nested Loops - Interserver Tips
August 4, 2025 - Home » Python » Everything You Need to Know About Python Nested Loops ... Nested loops are loops inside other loops. This means one loop runs completely, and during each run, another loop starts and runs too. It’s like putting a box inside another box—each time the outer box is opened, ...
🌐
Scaler
scaler.com › home › topics › python nested loops
Python Nested Loops - Scaler Topics
March 22, 2024 - Nested loop in Python involves the inclusion of one loop within another. This construct allows for iterating over elements in multiple dimensions, such as rows and columns of a matrix.
🌐
GeeksforGeeks
geeksforgeeks.org › loops-in-python
Loops in Python - For, While and Nested Loops - GeeksforGeeks
After the loop ends it prints "Inside Else Block" as the else block executes when the loop completes without a break. Python programming language allows to use one loop inside another loop which is called nested loop. Following section shows ...
Published   March 8, 2025
🌐
Programiz PRO
programiz.pro › resources › python-nested-loop
Understanding Nested Loops in Python with Logic Building
A nested loop is a loop inside another loop. The outer loop determines how many times the inner loop will execute. This is particularly useful for tasks involving multi-row or multi-column structures, such as grids or patterns.