Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
factorial, repeating
1.n = int(input())
3.factorial = 1
4.while n > 1:
6. factorial *= n
7. n -= 1
11.#I would like to write a code where I could repeat the action endlessly and ask the user for the number he wants to find the factorial. I've tried both "break" and "continue", just in the book I'm reading a summary of "while". I also wanted to write "input", but the code did not work
You can repeat endlessly with a while True loop
while True:
    # code to be repeated endlessly
I dont get the question completely but i gues this would help
def iter_factorial(n):
    n = input("Enter a number: ")
    factorial = 1
    if int(n) >= 1:
        for i in range (1,int(n)+1):
            factorial = factorial * i
        return factorial
num=int(input("Enter the number: "))

print("factorial of ",num," (iterative): ",end="")
If this does not help, try to go through this article factorial in python
I believe all is asked for is wrapping a loop around the loop. To elaborate slightly on Gribouillis:
while True:
        n = int(input('Enter number or done: '))
    except ValueError:
        break  # Quit if input is not a number

    factorial = 1
    for i in range(1, n+1):
        factorial *= i
    print(f'{n}! = {factorial}')
I hate to spoil the party:
import math
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is 2/3 not just .666 repeating? DocFro 4 787 Dec-12-2023, 09:09 AM
Last Post: buran
  repeating a user_input astral_travel 17 2,422 Oct-26-2022, 04:15 PM
Last Post: astral_travel
  if else repeating Frankduc 12 2,648 Jul-14-2022, 12:40 PM
Last Post: Frankduc
  Comparing recursion and loops using two scripts (basic factorial) Drone4four 3 2,300 Oct-11-2020, 06:48 PM
Last Post: deanhystad
  Python factorial code timebrahimy 4 76,304 Sep-27-2020, 12:23 AM
Last Post: timebrahimy
  factorial using recursive function spalisetty06 12 4,190 Aug-25-2020, 03:16 PM
Last Post: spalisetty06
  minor mistake in code for factorial spalisetty06 2 1,938 Aug-22-2020, 05:00 PM
Last Post: spalisetty06
  Factorial Code is not working when the given number is very long integer Raj_Kumar 2 2,362 Mar-31-2020, 06:40 PM
Last Post: deanhystad
  Want to print each iteration of factorial program sbabu 10 4,661 Jan-09-2020, 07:25 AM
Last Post: perfringo
  Factorial sketch(python 3) KingArthur526 1 2,008 Sep-25-2019, 01:51 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020