Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
factorial, repeating
#1
1.n = int(input())
2.
3.factorial = 1
4.while n > 1:
5.
6. factorial *= n
7. n -= 1
8.
9.print(factorial)
10.
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
Reply
#2
You can repeat endlessly with a while True loop
while True:
    # code to be repeated endlessly
Reply
#3
I dont get the question completely but i gues this would help
def iter_factorial(n):
    factorial=1
    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="")
print(iter_factorial(num))
If this does not help, try to go through this article factorial in python
Reply
#4
I believe all is asked for is wrapping a loop around the loop. To elaborate slightly on Gribouillis:
while True:
    try:
        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}')
Reply
#5
I hate to spoil the party:
import math
print(math.factorial(3))
Paul
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'.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is 2/3 not just .666 repeating? DocFro 4 687 Dec-12-2023, 09:09 AM
Last Post: buran
  repeating a user_input astral_travel 17 2,251 Oct-26-2022, 04:15 PM
Last Post: astral_travel
  if else repeating Frankduc 12 2,490 Jul-14-2022, 12:40 PM
Last Post: Frankduc
  matching a repeating string Skaperen 2 1,231 Jun-23-2022, 10:34 PM
Last Post: Skaperen
  Random Number Repeating Tzenesh 5 4,007 Jan-13-2021, 10:00 PM
Last Post: deanhystad
  Comparing recursion and loops using two scripts (basic factorial) Drone4four 3 2,232 Oct-11-2020, 06:48 PM
Last Post: deanhystad
  Python factorial code timebrahimy 4 67,088 Sep-27-2020, 12:23 AM
Last Post: timebrahimy
  factorial using recursive function spalisetty06 12 4,040 Aug-25-2020, 03:16 PM
Last Post: spalisetty06
  minor mistake in code for factorial spalisetty06 2 1,870 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,285 Mar-31-2020, 06:40 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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