0

I am very new to Python, and I am having trouble making a factorial program.

num = int(input("Enter a positive integer: "))

for i in range(num-(num-1), num+1):
    print(num*(i[1]))

Why can't I multiply by the first part of a range sequence? I would also like to know how to make it so that it automatically multiplies by the next part of i, (i[2]), automatically without having to manually write it down.

Noor M
  • 1
  • 1
  • 2
    First, `num-(num-1)` is always `1`, so your sequence is `range(1, num+1)`. Secondly, `i` is an integer and doesn't support indexing. You also seem to be missing a right parenthesis, but that might just be a copy-paste error. I'm unsure what you mean by your question though. – Ted Klein Bergman Jan 09 '21 at 01:58
  • 1
    In your code, which variable are you expecting to be a range. Are you trying to multiply a range of numbers? Then range(start, stop) will be the approach while multiplying the value from range with num. note that range goes from start to stop-1. – Joe Ferndz Jan 09 '21 at 02:21
  • 1
    Does this answer your question? [Function for Factorial in Python](https://stackoverflow.com/questions/5136447/function-for-factorial-in-python) – Joe Ferndz Jan 09 '21 at 02:22
  • I am trying to multiply num by the first number in a range sequence – Noor M Jan 09 '21 at 02:29
  • `result = 1; for i in range(2, num+1): result = result * i; print(result)` gives you your factorial I believe. – DylanYoung Jan 09 '21 at 04:42
  • It works, but can you explain how it works? – Noor M Jan 09 '21 at 14:55

0 Answers0