4
\$\begingroup\$

I'm working through an introduction to computer science book, this is not homework for me but me trying to learn in my spare time, I'm not asking for homework help!

The output is below, and this is all ok, but I'm just certain there is a more efficient way of doing this. I have been trying for many hours now and this is still the best solution I can come up with, if anyone can improve upon this and possibly explain how, I would highly appreciate it.

The code:

naught = 0
one = 0
two = 0
three = 0
four = 0
five = 0
six = 0
seven = 0
eight = 0
nine = 0

for i in range (0, 10):
    print("")
    for i in range (0,1):
        print(naught, end = " ")
    for i in range (0,1):
        print(one, end = " ")
        one += 1
    for i in range (0,1):
        print(two, end = " ")
        two += 2
    for i in range (0,1):
        print(three, end = " ")
        three += 3
    for i in range (0,1):
        print(four, end = " ")
        four += 4
    for i in range (0,1):
        print(five, end = " ")
        five += 5
    for i in range (0,1):
        print(six, end = " ")
        six += 6
    for i in range (0,1):
        print(seven, end = " ")
        seven += 7
    for i in range (0,1):
        print(eight, end = " ")
        eight += 8
    for i in range (0,1):
        print(nine, end = " ")
        nine += 9

The output is below and that is all correct, that's no problem.

0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9
0 2 4 6 8 10 12 14 16 18
0 3 6 9 12 15 18 21 24 27
0 4 8 12 16 20 24 28 32 36
0 5 10 15 20 25 30 35 40 45
0 6 12 18 24 30 36 42 48 54
0 7 14 21 28 35 42 49 56 63
0 8 16 24 32 40 48 56 64 72
0 9 18 27 36 45 54 63 72 81

As I said I'm getting the right answer, just I'm not satisfied that it's in the best way. Many thanks, Matt.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Is this literally a multiplication table generator? \$\endgroup\$
    – wkl
    Commented Dec 21, 2011 at 19:35
  • 2
    \$\begingroup\$ range(0,1) = range(1) = [0] = one iteration? I don't get the point of doing a single iteration for-loop \$\endgroup\$
    – julio.alegria
    Commented Dec 21, 2011 at 20:54
  • 2
    \$\begingroup\$ @julio.alegria this has got to be the first time I have seen anyone do that deliberately and not as a joke. \$\endgroup\$
    – Karl Knechtel
    Commented Dec 21, 2011 at 21:02

3 Answers 3

10
\$\begingroup\$
for i in range(10):
    for j in range(10):
        print i*j,
    print ""

Something like this, perhaps?

\$\endgroup\$
4
\$\begingroup\$

If you are just looking to generate that same output, simplify your problem a little:

for x in range(10):
  temp = ''

  for y in range(10):
    temp += str(x * y) + ' '

  print temp

Or if you want to get fancy:

for x in range(10):
  print ' '.join(str(x * y) for y in range(10))
\$\endgroup\$
2
\$\begingroup\$
naught = 0
one = 0
two = 0
three = 0
four = 0
five = 0
six = 0
seven = 0
eight = 0
nine = 0

Whenever you find yourself creating a number of variables that differ by a number, it means you really wanted a list. Instead, try doing something like:

numbers = [0, 0, 0, 0, 0, 0, 0, 0] # there are better ways, but I'm keeping it simple for now

Now you can use numbers[0], numbers[1], etc instead of the named variables.

for i in range (0, 10):

Odd space between range and (

    print("")
    for i in range (0,1):

You shouldn't use the same name for your loop index for loops inside each other. You also really shouldn't write a loop that only execute once.

        print(naught, end = " ")

    for i in range (0,1):
        print(one, end = " ")
        one += 1

If we get rid of the pointless loop once, and use the list we get something like

print(numbers[1], end = " ")
numbers[1] += 1

    for i in range (0,1):
        print(two, end = " ")
        two += 2

Let's do the same here

print(numbers[2], end = " ")
numbers[2] += 2

Hey! those are almost exactly the same! All that differs is the two. In fact, we are going to do that many more times. So we can use a loop

for j in range(1, 10):
    print(numbers[j]. end = " ")
    numbers[j] += j

See how every place that had the number now uses j. We put the code in a loop and we don't need to repeat it 9 times.

\$\endgroup\$

Your Answer

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