1

I just started to study Python. I must use Python3.7. Could someone show me a working factorial code? i tried some i found here but i always get this error:

=================== RESTART: C:\programozás\pytutorial.py ===================

Code:

def factorial(n):

      result = 1

      for i in range(1, n + 1):

            result *= i

      return result
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
Daryl
  • 31
  • 5

4 Answers4

2

Your code is working, even though you could simply use the math library:

import math
print(math.factorial(5))

The problem does not come from your script, so maybe you should try to reinstall your python, and avoid paths with special characters as Adam Toth pointed out.

Update: to get the input and return a factorial as asked in comments

import math
print(math.factorial(int(input(">>"))))
olinox14
  • 6,177
  • 2
  • 22
  • 39
  • the code you sent will show 5! How can i make a code that will scan the numer i input and will show it's factorial? – Daryl Feb 04 '19 at 16:52
  • What? The code I wrote will print **the factorial of 5**, not 5, that's the role of the [factorial function](https://docs.python.org/3.7/library/math.html#math.factorial) – olinox14 Feb 04 '19 at 16:56
  • Oh, sorry, I did'nt saw the **!** char after your 5. – olinox14 Feb 04 '19 at 16:58
  • i reinstalled it, changed the folder and i have the same problem: >>> =================== RESTART: C:\programming\pytutorial.py =================== >>> – Daryl Feb 04 '19 at 17:05
  • import math print(math.factorial(int(input(">>")))) What to do with this code to get the answer like Factorial of [n] is [i] – Daryl Feb 04 '19 at 17:15
1

The problem is most likely caused because you have a special character in the path to the .py file. So should use a folder like C:\programming, or anything without a special character, like 'á'.

It's very important to do like this, even if it does not solve your current problem, it can prevent many more in the future.



Ps.: Jó kiszúrni magyar programozót is :)

Annosz
  • 992
  • 6
  • 23
  • Esetleg tudnál segíteni egy kicsit? (face, discord vagy valami) Akkor nem zavarnánk magyarul szegény angolokat. :) – Daryl Feb 04 '19 at 16:42
  • @Daryl I know it is nice to meet people from your own country, but please speak English on SO ;-). – Bart Friederichs Feb 04 '19 at 16:42
  • I just asked him to speak on an other platform so we do not disturb you with our language. :) – Daryl Feb 04 '19 at 16:44
0

I see a related (older) thread here about this error

For the logic: We have to consider:

  1. Negative numbers
  2. Zero
  3. Positive numbers

So one way to write it will be:

def factorial(n):
 if n < 0:
    result = "Factorial doesn't exist for negative numbers"
 elif n == 0:
    result = 1
 else:
    result = 1
    for i in range(1, n + 1):
        result *= i
 return result

You can try the concept of recursion as well.

To get the factorial of a number "num":

print(factorial(num))

Make sure you indent the code properly, indentation is important in python.

Hope it helps!

  • I tried to run it. I get this error: =================== RESTART: C:\programming\pytutorial.py =================== – Daryl Feb 04 '19 at 17:11
  • I see a related thread [here](https://stackoverflow.com/questions/33771625/simple-python-gui-program-wont-run-says-restart) for the same error.. – Neethu Mohandas Feb 04 '19 at 17:14
  • Thanks! So it has to contain print? – Daryl Feb 04 '19 at 17:17
  • How can i make this in python?: printf("Enter a number: " scanf("%d", num) – Daryl Feb 04 '19 at 17:22
  • if you are calling that function somewhere, it will return the result, otherwise if you want to see the result in console, yes you have to print it.. To accept input: `num = int(input("Enter a number: "))` – Neethu Mohandas Feb 04 '19 at 17:31
  • Thanks! How can i use it in a sentence? Like: `import math num = int(input("Which numbers factorial do you need?: ")) print (math.factorial(num))` <- I need to print -> Factorial of [num] is [result] – Daryl Feb 04 '19 at 17:39
0

Python Code of Factorial Using Recursive Function:

def factorial(n):
    if n <= 1:
        return 1
    else:
        return n * factorial(n-1)

factorial(5)

Note: The First Condition will only meet when the input is 0 or 1 and in else block n will be recursively multiplying n * n - 1.