문제

I have to write a program that outputs the numbers 1-100. In this kind of format:

 1   11   21   31   41   51   61   71   81   91
 2   12   22   32   42   52   62   72   82   92
 3   13   23   33   43   53   63   73   83   93
 4   14   24   34   44   54   64   74   84   94
 5   15   25   35   45   55   65   75   85   95
 6   16   26   36   46   56   66   76   86   96
 7   17   27   37   47   57   67   77   87   97
 8   18   28   38   48   58   68   78   88   98
 9   19   29   39   49   59   69   79   89   99
10   20   30   40   50   60   70   80   90  100

That's not the problem but in the next Step I have to replace each number that can be divided by 3 or contains a 3 into a specific word. Biff in my case. So the output should look like this:

   1   11 Biff Biff   41 Biff   61   71 Biff   91
   2 Biff   22 Biff Biff   52   62 Biff   82   92
Biff Biff Biff Biff Biff Biff Biff Biff Biff Biff
   4   14 Biff Biff   44 Biff   64   74 Biff   94
   5 Biff   25 Biff Biff   55   65 Biff   85   95
Biff   16   26 Biff   46   56 Biff   76   86 Biff
   7   17 Biff Biff   47 Biff   67   77 Biff   97
   8 Biff   28 Biff Biff   58   68 Biff   88   98
Biff   19   29 Biff   49   59 Biff   79   89 Biff
  10   20 Biff   40   50 Biff   70   80 Biff  100

And that's where I'm stuck. Right now my code looks like this:

number=1
while number< 11:
    if (number % 3 == 0):
        print("Biff")
        number +=1
    else:
        print('{0:4d} {1:4d} {2:4d} {3:4d} {4:4d} {5:4d} {6:4d} {7:4d} {8:4d} {9:4d}'.format(number, number+10, number+20, number+30, number+40, number+50, number+60, number+70, number+80, number+90))
        number +=1

And the output looks like this:

   1   11   21   31   41   51   61   71   81   91
   2   12   22   32   42   52   62   72   82   92
Biff
   4   14   24   34   44   54   64   74   84   94
   5   15   25   35   45   55   65   75   85   95
Biff
   7   17   27   37   47   57   67   77   87   97
   8   18   28   38   48   58   68   78   88   98
Biff
  10   20   30   40   50   60   70   80   90  100

I'm trying to replace the numbers that can be divided by 3. But instead of replacing the single number it's replacing the whole line.

도움이 되었습니까?

해결책

Here's my modification of your code:

for number in range(1, 11):
    line_contents = []
    for j in range(10):
        n = number + j * 10
        if (n % 3 == 0):
            line_contents.append("Biff")
        else:
            line_contents.append("{0:4d}".format(n))
    print(" ".join(line_contents))

This gives me:

   1   11 Biff   31   41 Biff   61   71 Biff   91
   2 Biff   22   32 Biff   52   62 Biff   82   92
Biff   13   23 Biff   43   53 Biff   73   83 Biff
   4   14 Biff   34   44 Biff   64   74 Biff   94
   5 Biff   25   35 Biff   55   65 Biff   85   95
Biff   16   26 Biff   46   56 Biff   76   86 Biff
   7   17 Biff   37   47 Biff   67   77 Biff   97
   8 Biff   28   38 Biff   58   68 Biff   88   98
Biff   19   29 Biff   49   59 Biff   79   89 Biff
  10   20 Biff   40   50 Biff   70   80 Biff  100

The key change is to have a variable n that contains each number in turn and to test whether this is divisible by 3. You were simply testing whether number was divisible by 3, i.e. only the number at the beginning of the line.

I've also tried to tidy up how you generate each line by storing a list of strings and building these up in an inner loop. After each inner loop I concatenate the strings and print the result.

Feel free to ask about any parts that aren't clear.

다른 팁

for i in range(10):
    for j in range(10):
        n = 1+i+j*10
        print "{0:4d}".format(n) if (n) % 3 != 0 and  '3' not in str(n) else "Biff",
    print

gives

$ python t.py 
   1   11 Biff Biff   41 Biff   61   71 Biff   91
   2 Biff   22 Biff Biff   52   62 Biff   82   92
Biff Biff Biff Biff Biff Biff Biff Biff Biff Biff
   4   14 Biff Biff   44 Biff   64   74 Biff   94
   5 Biff   25 Biff Biff   55   65 Biff   85   95
Biff   16   26 Biff   46   56 Biff   76   86 Biff
   7   17 Biff Biff   47 Biff   67   77 Biff   97
   8 Biff   28 Biff Biff   58   68 Biff   88   98
Biff   19   29 Biff   49   59 Biff   79   89 Biff
  10   20 Biff   40   50 Biff   70   80 Biff  100
map(lambda x: x * (x % 3 == 0) or "Biff", you_list)
entries = ['Biff' if n % 3 == 0 or n % 10 == 3 else '{:4d}'.format(n) for n in range(1, 101)]

for row in range(10):
    print(' '.join(e for e in entries[row::10]))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top