0

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.

Tony
  • 9,672
  • 3
  • 47
  • 75

3 Answers3

1

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]:
Nishant Nawarkhede
  • 8,234
  • 12
  • 59
  • 81
  • This worked perfectly! I can't believe that I missed such an obvious mistake! Thank you for your help and have a great rest of your day. – Gunnar Larson Sep 12 '19 at 11:34
0

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

for i in range(1, 13):       
  print(N*i) 
Tony
  • 9,672
  • 3
  • 47
  • 75
  • 2
    N = N * i would give 6, 12, 36, .. instead wouldn't it? – R. Jüntgen Sep 12 '19 at 11:30
  • Yes, sorry. My mistake I copied the code from the question and forgot to remove the assignment! Oops! (serves me right for quickly answering SO questions when I should be working! ;) – Tony Sep 12 '19 at 11:32
0

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

Community
  • 1
  • 1
rohitwtbs
  • 509
  • 5
  • 17