1

I'm trying to get user input (x), and then print x rows, and x/2 columns. I cannot get it to work. I keep only getting two columns of data, with duplicate rows. I'm supposed to complete this using a nested for loop that only loops once (per assignment). I have the following code:

x = int(input("Enter a number: "))
val = x//2

for row in range(x):
    for column in range(val):
        print(row, column)

My goal is to have this type of output if the user inputs 4, for example:

1       1       2
2       1       2
3       1       2
4       1       2

But, this is what I'm getting if a user inputs 4:

0 0
0 1
1 0
1 1
2 0
2 1
3 0
3 1
2
  • You want a column with the index (1...x), then x//2 more columns? Commented Jun 27, 2021 at 22:06
  • Yes, that is the general idea I believe Commented Jun 27, 2021 at 22:08

3 Answers 3

4

This should do the job: (end = some_string replaces the CR/LF by some_string)

x = int(input("Enter a number: "))
val = x//2

for row in range(1,x+1):
    print(row, end = " ")
    for column in range(1,val+1):
        print(column, end = " ")
    print()
Sign up to request clarification or add additional context in comments.

Comments

1

Check this one, try to understand the logic. As you are currently learning, I won't explain now, but if you need help, let me know.

x = int(input("Enter a number: "))
val = x//2

for row in range(x):
    print(row+1, end = ' ')
    for column in range(val):
        print(column +1, end = ' ')
    print('\n')

1 Comment

If it solves your problem, can you please accept the answer as the correct one?? click the icon under the upvote/downvote button.
0

This is should yield the desired output.

x = int(input("Enter a number: "))
val = x//2

for row in range(1, x + 1):
    for col in range(1, val + 1):
        print(col, end=' ')
    print()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.