WAP in python script to input a multidigit number and find each of the number's factorial

Question:

The output shows a different result. Yes, the factorials of those numbers are right but the numbers outputted aren’t right.

Here’s the code:
input:

n = int(input("Enter a number: "))
s = 0
fact = 1
a = 1
for i in range(len(str(n))):
    r = n % 10
    s += r
    n //= 10
    while a <= s:
        fact *= a
        a += 1

    print('The factorial of', s, 'is', fact)

Output:

Enter a number: 123
The factorial of 3 is 6
The factorial of 5 is 120
The factorial of 6 is 720
Asked By: Raleigh

||

Answers:

You can use map function to get every single digit from number:

n = int(input("Enter a number: "))
digits = map(int, str(n))

for i in digits:
    fact = 1 
    a = 1
    while a <= i:
        fact *= a
        a += 1
    print('The factorial of', i, 'is', fact)
Answered By: Karol

Ok, apart from the fact that you print the wrong variable, there’s a bigger error. You are assuming that your digits are ever increasing, like in 123. Try your code with 321… (this is true of Karol’s answer as well). And you need to handle digit zero, too

What you need is to restart the calculation of the factorial from scratch for every digit. For example:

n = '2063'
for ch in reversed(n):
    x = int(ch)
    if x == 0:
        print(f'fact of {x} is 1')
    else:
        fact = 1
        for k in range(2,x+1):
              fact *= k
        print(f'fact of {x} is {fact}')
Answered By: gimix

You’re confusing yourself by doing it all in one logic block. The logic for finding a factorial is easy, as is the logic for parsing through strings character by character. However, it is easy to get lost in trying to keep the program "simple," as you have.

Programming is taking your problem, designing a solution, breaking that solution down into as many simple, repeatable individual logic steps as possible, and then telling the computer how to do every simple step you need, and what order they need to be done in to accomplish your goal.

Your program has 3 functions.

The first is taking in input data.

input("Give number. Now.")

The second is finding individual numbers in that input.

for character in input("Give number. Now."):
     try:
         int(character)
     except:
         pass    

The third is calculating factorials for the number from step 2. I won’t give an example of this.

Here is a working program, that is, in my opinion, much more readable and easier to look at than yours and others here. Edit: it also prevents a non numerical character from halting execution, as well as using only basic Python logic.

def factorialize(int_in):
    int_out = int_in
    int_multiplier = int_in - 1
    while int_multiplier >= 1:
        int_out = int_out * int_multiplier
        int_multiplier -= 1
    return int_out

def factorialize_multinumber_string(str_in):
    for value in str_in:
        print(value)
        try:
            print("The factorial of {} is {}".format(value, factorialize(int(value))))
        except:
            pass

factorialize_multinumber_string(input("Please enter a series of single digits."))            
Answered By: Sean Lindorff
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.