0

I tried to make this simple program to find factorial of inputted number. But there is an error somewhere i could not figure out: while i use raw_input() function it displays 'none' output and if i use input() function name error is displayed. Help me to find bug .

program_active=True
while program_active:
    def fact(n):
        if n==0:
            return 1
        elif not isinstance(n, int):
            while not isinstance(n, int):
                print "Factorial is defined only for Positive integers"
                try:
                    n=int(raw_input())
                except ValueError as ve:
                    pass
        elif n<0:
            print "Factorial is defined only for Positive integers"
        else:
            step1=fact(n-1)
            step2=n*step1
            return step2
    print"input a number to check its factorial"
    n=raw_input()
    print fact(n)
    print
    print
    print
    print "do yo want to run the program again? (y/n)"
    run=raw_input()
    while run[0].upper()!='Y' and run[0].upper()!='N':
        print"****invalid input****"
        run=raw_input('say y/n \n')
    if run[0].upper()=='N':
        program_active=False
        print "goodbye"

here is error while using raw_input() function


> input a number to check its factorial 
> 8 
> Factorial is defined only for Positive integers 
> 8 
> None
> 
> 
> 
> do yo want to run the program again? (y/n) n goodbye

JayPeerachai
  • 3,499
  • 3
  • 14
  • 29
Ashim Paudel
  • 19
  • 1
  • 3
  • Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – SiHa Aug 24 '20 at 12:59

1 Answers1

1

Your code DOESN'T WORK.

8 that you input first is string so it will goes through the condition below:

elif not isinstance(n, int):
     while not isinstance(n, int):
          print "Factorial is defined only for Positive integers"
          try:
              n=int(raw_input())
          except ValueError as ve:
              pass

After that when you input the new 8 which is then converted to int, it will break the while loop and go out the fact() function without going the part of computing factorial in else condition shown below.

else:
     step1=fact(n-1)
     step2=n*step1
     return step2

To FIX this problem, I would suggest you to move the part of input validation out from the def fact().

CODE:

program_active=True
while program_active:
    def fact(n):
        if n==1:
            return n
        elif n==0:
            return 1
        else:
            step1=fact(n-1)
            step2=n*step1
            return step2
    while True:
      print"input a number to check its factorial"
      try:
        n=int(raw_input())
      except:
        print("Factorial is defined only for integer")
        continue
      if n < 1:
        print 'Factorial is defined only for Positive integer'
        continue 

      x = fact(n)
      print x

OUTPUT:

> input a number to check its factorial 
> 8  
> 40320

This code should work.

JayPeerachai
  • 3,499
  • 3
  • 14
  • 29