Can anyone tell me what's wrong with my code here?

1.7k Views Asked by At

I have some code written here for a Codio challenge that I can't seem to figure out how to correctly write. I'm fairly new to Python and for some reason this times table loop challenge is killing me. The code is as follows:

# Get N from the command line
import sys
N = int(sys.argv[1])


for i in range(1, 13):  
  N = N + i     
  print(str(N))         

The question asks:

We will provide you with a number N. Output the times table for that number from 1 to 12.

So, if we pass in 6, you should output 6, 12, 18, 24 … 66, 72

Any help would be greatly appreciated.

3

There are 3 best solutions below

2
On

You are adding the loop index to the number N, you need to multiply instead.

for i in range(1, 13):       
  print(N*i) 
1
On

Just use index * N

In [11]: N = 6

In [12]: for i in range(1, 13):
    ...:   print(N*i)
    ...:
    ...:
    ...:
6
12
18
24
30
36
42
48
54
60
66
72

In [13]:
0
On

for i in range(1, 13):
print(N*i)