Multiple Python input factorial

Asked

Viewed 385 times

2

I need to write a program in the Python language that reads multiple entries, and when receiving the "-1" input, the program prints the factorial of the respective input numbers.

Detail: need to use loop function while, since it is part of the current subject I study. I am writing as follows:

while True:
    n = int(raw_input())
    if n >= 0:
        continue
    if n == -1:
        break
    numeroCalc = n
    fatorial = 1
    while numeroCalc > 0:
        fatorial = fatorial * numeroCalc
        numeroCalc = numeroCalc - 1
    print fatorial

But it’s not working.

  • What’s not working? You can use a list or something?

  • The program does not print the Factorials... The program closes after the "-1" entry, and does not print the factorials.

  • I believe I can use a list, as long as I use the while function

1 answer

3


by what you said you want, it should be +/- so the code

numb = "\n"

while True:
    n = int(raw_input())
    if n == -1:
        break
    numeroCalc = n
    fatorial = 1
    while numeroCalc > 0:
        fatorial = fatorial * numeroCalc
        numeroCalc = numeroCalc - 1
    numb = numb+str(fatorial)+"\n"

print numb
  • Then I should just remove the continue and keep the break?

  • 1

    When removing the continue, the program prints the factorial right after the input, and that’s not how it should be. I must put more than one entry, and the program must read them, and only print the factorial of each entry after the "-1" entry, which it represents: finished, print the factorial of each entry, each in a row. Thank you for your attention :D

  • I think I get it, try the code I put in

  • 1

    It works, but I can not use because it has terms that have not yet been addressed in the classes I attended... como n.append() e del().

  • that should solve

  • It works! : D Now I just have to understand kkk but it seems simple, thank you very much!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.