Python Forum
Am i doing something wrong with this piece of code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Am i doing something wrong with this piece of code
#1
i am trying to get an output as follows:
Quote:1 1 1
1 1 2
1 1 3
1 2 2
1 2 3
1 2 1
1 3 3
1 3 1
1 3 2
2 2 2
2 2 3
2 2 1
2 3 3
2 3 1
2 3 2
2 1 1
2 1 2
2 1 3
3 3 3
3 3 1
3 3 2
3 1 1
3 1 2
3 1 3
3 2 2
3 2 3
3 2 1
with the following code
for i in range(1, 4):
    for j in range(i, i + 3):
        if (j > 3):
            jdisplay = j % 3

        else:
            jdisplay = j
        for k in range(j,j+3):
            if(k>3):
                kdisplay=k%3
            else:
                kdisplay=k
            print(i,jdisplay,kdisplay)
but i am getting output as below
Quote:1 1 1
1 1 2
1 1 3
1 2 2
1 2 3
1 2 1
1 3 3
1 3 1
1 3 2
2 2 2
2 2 3
2 2 1
2 3 3
2 3 1
2 3 2
2 1 1
2 1 2
2 1 0
3 3 3
3 3 1
3 3 2
3 1 1
3 1 2
3 1 0
3 2 2
3 2 0
3 2 1

The difference is the 0 being printed instead of 3 (in bold) in the 3rd column, in the piece of code i have written i have asked it to do a modulo division only if the number is greater than 3, but is it doing even if it is equal to 3? or am i missing something
Reply
#2
I'm guessing because theres no remainder.
for k in range(j,j+3):
Starts at index 3 and ends on 6.
6%3 = 0
You could try checking it first and assign 3 if it has no remainder.
There's probably a better way of doing it but this should work.
kdisplay= k%3 if (k%3) else 3

for i in range(1, 4):
    for j in range(i, i + 3):
        if (j > 3):
            jdisplay = j % 3
 
        else:
            jdisplay = j
        for k in range(j,j+3):
            kdisplay= k%3 if (k%3) else 3
            print(i,jdisplay,kdisplay)
Reply
#3
wow, this works well, and oh my god your first post is the reply to my question...awesome ..

but this seems to be hard coded approach, is there a better way of doing it?
Reply
#4
I've been hanging around the forum for a few weeks reading different posts but forgot to register ha.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 494 Nov-07-2023, 04:32 PM
Last Post: snippsat
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 726 Sep-05-2023, 12:50 PM
Last Post: ToniE
  First piece of code Bearwulf 2 773 Aug-02-2023, 04:03 PM
Last Post: deanhystad
  Something wrong with my code FabianPruitt 5 872 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,588 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,269 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,822 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,582 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Understanding a piece of code Michael1 4 1,431 Jan-20-2022, 07:14 PM
Last Post: Michael1
  Wrong code in Python exercise MaartenRo 2 1,542 Jan-01-2022, 04:12 PM
Last Post: MaartenRo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020