Python Forum
Python factorial code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python factorial code (/thread-29953.html)



Python factorial code - timebrahimy - Sep-26-2020

Hi all,
I am new to scientific python. While trying to follow the code below i always get "Factorial does not exist" response. I am using python 3.8 but the code is written in python 2.7 and it works. I have broken the code and ran it piecewise, still the code will not work properly when i enter i.e. 6 for n etc...integers greater than 1. I am out of tricks so please help me understand this factorial code: Thank you very much in advance!

f = 1
n = input("Enter a value for n:")
if isinstance(n, int): #check whether the value is integer
    if n == 1 or 0:
        print ("Execution at if ")
        print ("The value of {}! = {}" .format(n, f))
        exit
    elif n < 0:
        print ("Execution at elif ")
        print ("n is negative")
    else:
        print ("Executoin at else")
        for i in range(2, n+1):
            f *= i
        print ("The value of {}! = {}" .format(n, f))
else:
    print ("Factorial does not exist")



RE: Python factorial code - Larz60+ - Sep-26-2020

n is not an integer, it's a string (numeric string if you entered a number).
It must be cast to integer as follows:
n = int(input("Enter a value for n:"))

better code would be:
while True:
    try:
        n = int(input("Enter a value for n: "))
        break
    except ValueError:
        print("Please use numeric values only")



RE: Python factorial code - timebrahimy - Sep-26-2020

The answer for n = 6 should be as follows:
Enter a value for n:6
Execution tr else
The value of 6! is 720


RE: Python factorial code - deanhystad - Sep-26-2020

In Python 2.7 the input command evaluated what you typed. If you typed 3.14 it would evaluate the string '3.14. and return the float number 3.14. If you type 42 it would evaluate the string '42' and return an integer number 42. If you typed a it would look for a variable named a and return the value of a.

In Python 3 the input command stopped doing the evaluation part and left that to be programmed. In Python 3 if you input 3.14 it remains the string 3.14 until you do something to convert the string to a number.


RE: Python factorial code - timebrahimy - Sep-27-2020

Thanks Lars, type casting to integer seems to do the trick!
Regards,
tim


(Sep-26-2020, 09:42 PM)timebrahimy Wrote: Hi all,
I am new to scientific python. While trying to follow the code below i always get "Factorial does not exist" response. I am using python 3.8 but the code is written in python 2.7 and it works. I have broken the code and ran it piecewise, still the code will not work properly when i enter i.e. 6 for n etc...integers greater than 1. I am out of tricks so please help me understand this factorial code: Thank you very much in advance!

f = 1
n = input("Enter a value for n:")
if isinstance(n, int): #check whether the value is integer
    if n == 1 or 0:
        print ("Execution at if ")
        print ("The value of {}! = {}" .format(n, f))
        exit
    elif n < 0:
        print ("Execution at elif ")
        print ("n is negative")
    else:
        print ("Executoin at else")
        for i in range(2, n+1):
            f *= i
        print ("The value of {}! = {}" .format(n, f))
else:
    print ("Factorial does not exist")