Search code examples
pythonwhile-loopfactorial

How do I get a while loop that is calculating the factorial to disregard the previous factorials?


Update: I have solved the problem of compounding factorials by resetting the factorial to 1 at the start of the loop and eliminating unnecessary code. Here is the new version.

more = "yes"
while more == "yes":
    n = int(input("Enter a number to get its factorial: "))
    if n >= 0:
        factorial = 1
        for num in range(1, n+1):
            factorial *= num
        print(n,'!=', factorial)
        more = input("Would you like to get another number's factorial?:")
    else:
        print("Factorials are not defined for negative integers.")
print("Thank You!")

Solution

  • You need to reset the factorial variable to 1 at the start of the while loop.

    Also, you'll want to check if n is positive for every input, as it currently only does so for the first.