Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:03:36+00:00 2026-06-11T11:03:36+00:00

I have to prompt a user to input a value n >= 3 such

  • 0

I have to prompt a user to input a value n >= 3 such that my python 3.2 program can print a n by n pattern.

Example, n=4 ==> Pattern:

x..x
.xx.
.xx.
x..x

Another example, n=5 ==> Pattern:

x...x
.x.x.
..x..
.x.x.
x...x

Notice that the x get closer to each other after each row. After the middle row the get further from each other and finally reach their respective ends.

Please help me with this problem. I know I have to use branching and looping but I just can’t figure the logic behind the pattern.

I’ve managed to print out a n by n pattern but it looks like this:
Example n=4 ==> Pattern:

xx..  
xx..
xx..
xx..

My code looks like this:

    pattern_size = int(input("Please enter pattern size:"))
    while pattern_size < 3:
    print("Pattern size should be at least 3")
    pattern_size = int(input("Please enter pattern size:"))

    for level in range(1,pattern_size+1):
        for num in range(2):
            print("x", end="")

        num_dot = pattern_size - 2
        for num in range(num_dot):
            print(".", end="")

        print()

Can someone advice me on how to print the “.” and “x” alternately?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:03:38+00:00Added an answer on June 11, 2026 at 11:03 am

    Here is the code that works for python2.7. The trick here is one of the “x” always shifting left, while the other one is always shifting right.

    def pattern_writer(pattern_size):
        first_x = 0
        last_x = pattern_size - 1
        while first_x < pattern_size and last_x > -1:
            for t in range(pattern_size):
                if t == first_x or t == last_x:
                    print "x",
                else:
                    print ".",
            print "\n"
            first_x += 1
            last_x -= 1
    

    Here is another solution using lists:

    def pattern_writer2(pattern_size):
        for t in range(pattern_size):
            pattern = list("." * pattern_size)
            pattern[t] = "x"
            pattern[-(t+1)] = "x"
            print "".join(pattern)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web page that prompts for user input via DropDownLists in some
I have this little credit card validation program where the user has to input
I am have a piece of code that prompts for user input and works
I have created a View which prompts the user to input some records with
I have a standalone application in which I have to prompt the user with
OK, I have this assignment and I have to prompt the user for data
I have a large webform, and would like to prompt the user to login
I'm interested in using a JavaScript Prompt to have the user enter data. Any
I have a main report that prompts the user to enter two parameters (@ORI
I have implemented a LoginAccess class that prompts the user to enter their active

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.