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


Messages In This Thread
Am i doing something wrong with this piece of code - by pythoneer - Mar-02-2018, 01:30 PM

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 638 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 878 Sep-05-2023, 12:50 PM
Last Post: ToniE
  First piece of code Bearwulf 2 876 Aug-02-2023, 04:03 PM
Last Post: deanhystad
  Something wrong with my code FabianPruitt 5 1,069 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,856 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,476 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 2,137 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 3,015 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Understanding a piece of code Michael1 4 1,584 Jan-20-2022, 07:14 PM
Last Post: Michael1
  Wrong code in Python exercise MaartenRo 2 1,632 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