-2

I'm just trying to do some looping functions in Python, however I am pretty much stuck here.

I don't know if it should be a nested or simultaneous loop, and if so, how to use it probably.

Python is telling me (ValueError: too many values to unpack)

Here is my code:

rows = 16

for i in range(rows):

    for a,b,c,d,e,f,g,h,j in range(1, 17), range(18, 34), range(35, 40), range(41, 57), range(58, 74), range(75, 91), range(92, 108), range(109, 125), range(126, 127):
        print '{0:4d} {1:4d} {2:4d} {3:4d} {4:4d} {5:4d} {6:4d} {7:4d}'.format(a, b, c, d, e, f, g, h, j)

I basicly want the application to print out the range of numbers from 1 to 128 in this layout over 16 rows with the numbers going horisontal:

  1    2    3    4    5    6    7    8
  9   10   11   12   13   14   15   16
 122  122  122  122  122  122  122  122
 123  123  123  123  123  123  123  123
 124  124  124  124  124  124  124  124
 125  125  125  125  125  125  125  125
 126  126  126  126  126  126  126  126
 127  127  127  127  127  127  127  127
 128  128  128  128  128  128  128  128
 128  128  128  128  128  128  128  128
 124  124  124  124  124  124  124  124
 125  125  125  125  125  125  125  125
 126  126  126  126  126  126  126  126
 127  127  127  127  127  127  127  127
 128  128  128  128  128  128  128  128
 128  128  128  128  128  128  128  128
Emil Elkjær
  • 685
  • 1
  • 9
  • 31
  • Do you mean you want the numbers going vertically? It looks from the code like you're attempting to have the numbers increase down each column, but your desired output doesn't look anything like that. – David Robinson Jan 15 '15 at 16:39
  • Am I reading your output correctly? Start at 121, go to 128 and double it, go back to 124, go to 128 and double it? – JonB Jan 15 '15 at 16:41
  • JonB, this was just a quickly generated example :) I want it to continue to grow so it starts from 1 and ends at 128, for example: First line: 1 2 3 4 5 6 7 8 Second line: 9 10 11 12 13 14 15 16 – Emil Elkjær Jan 15 '15 at 17:00
  • @EmilElkjærNielsen - Edited my answer per your comment. Please check it out. – JonB Jan 15 '15 at 17:26

6 Answers6

2

This would be a much less convoluted way:

def printRows(startNum, endNum, numCols=8):
    for n in xrange(startNum, endNum, numCols):
        for c in xrange(numCols):
            curNum = n + c
            if curNum > endNum: break
            print '{0: >4}'.format(str(curNum)),
        print

In [57]: printRows(1,128)

   1    2    3    4    5    6    7    8
   9   10   11   12   13   14   15   16
  17   18   19   20   21   22   23   24
  25   26   27   28   29   30   31   32
  33   34   35   36   37   38   39   40
  41   42   43   44   45   46   47   48
  49   50   51   52   53   54   55   56
  57   58   59   60   61   62   63   64
  65   66   67   68   69   70   71   72
  73   74   75   76   77   78   79   80
  81   82   83   84   85   86   87   88
  89   90   91   92   93   94   95   96
  97   98   99  100  101  102  103  104
 105  106  107  108  109  110  111  112
 113  114  115  116  117  118  119  120
 121  122  123  124  125  126  127  128

This method also allows for a more generalized approach that can be used to print just about any table given startNum, endNum, and numCols. For example,

In [97]: printRows(37,129,10)
  37   38   39   40   41   42   43   44   45   46
  47   48   49   50   51   52   53   54   55   56
  57   58   59   60   61   62   63   64   65   66
  67   68   69   70   71   72   73   74   75   76
  77   78   79   80   81   82   83   84   85   86
  87   88   89   90   91   92   93   94   95   96
  97   98   99  100  101  102  103  104  105  106
 107  108  109  110  111  112  113  114  115  116
 117  118  119  120  121  122  123  124  125  126
 127  128  129

In [98]: printRows(1,15,3)
   1    2    3
   4    5    6
   7    8    9
  10   11   12
  13   14   15

In [99]: printRows(9,27,4)
   9   10   11   12
  13   14   15   16
  17   18   19   20
  21   22   23   24
  25   26   27
JonB
  • 836
  • 1
  • 11
  • 15
1

Multiple issues in your code. Mainly:

  1. If you just need to unpack the values, you don't need loop:

    simply a , b = range(0,100), range(101,200) works

  2. print '{0:4d}'.format(a) does not expect a to be a list.

For me below code works:

rows = 16

for i in range(rows):
    a,b,c,d,e,f,g,h,j = range(1, 17), range(18, 34), range(35, 40), range(41, 57), range(58, 74), range(75, 91), range(92, 108), range(109, 125), range(126, 127)
    print '{0:4d} {1:4d}'.format(10,100)

I understand that this is not the output that you want, but it should certainly help.

Update:

rows = 16
for i in range(rows):
    k = i*8+1
    for j in range(k,k+8):
        print '{0:4d}'.format(j),
    print " "
Ankit Jaiswal
  • 22,859
  • 5
  • 41
  • 64
  • Hi, thanks for your answer. I basically want it to count upward, thats why I added the ranges - Like in the format I showed, it should count from 1 to 128 and then print it in this format where first line would be "1 2 3 4 5 6 7 8" and second line would be "9 10 11 12 13 14 15 16" and continue like that until 128 has been reached – Emil Elkjær Jan 15 '15 at 17:04
  • @EmilElkjærNielsen I've updated the answer to produce your desired output in the simplest form (though the code can surely be improved). – Ankit Jaiswal Jan 15 '15 at 17:25
  • Worked flawless! Thanks a lot – Emil Elkjær Jan 15 '15 at 17:27
  • Just a single question more - Trying to use chr() on the print output j, it throws error: ValueError: Unknown format code 'd' for object of type 'str' – Emil Elkjær Jan 15 '15 at 17:30
  • You mean str() right? in `'0:4d'` d denotes a digit, for string you can use `s` instead. – Ankit Jaiswal Jan 15 '15 at 17:38
  • Wops I fixed it. Just replaced d with C - Worked like a charm! Thanks a lot for your help :) – Emil Elkjær Jan 15 '15 at 17:40
1

A derivation of @JonB's solution. It includes an optional function parameter to compute the value based on the current index n. You could extend this example so that the equation took the start, end, num_column, and a dictionary as addtional arguments.

def x(n):
    return n

def print_rows(start, end, num_columns = 8, equation = x):
    for n in xrange(start, end + 1):
        print '{0: >4}'.format(str(equation(n))),
        if ( ((n - (start - 1)) % num_columns) == 0 or n == end ):
            print

# these return the same values as JonB's solution
print_rows(1, 128)
print_rows(37,129)
print_rows(1,15,3)

def multiply_by_three(n):
    return n * 3

print_rows(1,20,4,multiply_by_three)
# returns
   3    6    9   12
  15   18   21   24
  27   30   33   36
  39   42   45   48
  51   54   57   60 
Steve Wilhelm
  • 6,200
  • 2
  • 32
  • 36
0

This is invalid:

for i , b in range(0,10) , range(10,20):

You can't unpack 2 loops in a single for statement.

Loupi
  • 550
  • 6
  • 14
-1

In your print statement you have 8 {} and 9 values

vaultah
  • 44,105
  • 12
  • 114
  • 143
user2097159
  • 862
  • 5
  • 13
-1
x = []
for e in range(1, 129):
    x.append(e)
strt = 0
end = 8
for i in range(0, 16):
    print(*x[strt:end])
    strt = end
    end += 8



#Output:
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64
65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88
89 90 91 92 93 94 95 96
97 98 99 100 101 102 103 104
105 106 107 108 109 110 111 112
113 114 115 116 117 118 119 120
121 122 123 124 125 126 127 128
Tysa
  • 9
  • 3