Welcome to Interactive Programming Forums, where you can ask questions, receive answers from other members of the community, post code snippets and post exercies.

I am have a problem with understanding a while loop, how to terminate it?

–2 votes
How do I terminate an event-controlled while loop? say I have to write a program that does factorials for integers?
The way I did it was like this:

n = int(raw_input())
a = 0
while n !=0
    n = n-a
    print n*n
n = int(raw_input())

I thought the last line was going to terminate the loop by constantly updating n for every loop. And how do I make this to be one correct answer?
posted 5 years ago in Python by simphiwemakola12 (1,010 points)

1 Answer

0 votes
n = int(raw_input())
a = 1
while n !=0
    n = n-a
    print n*n
    a = a +1
answered 5 years ago by Aguibou Ndiaye (240 points)
This doesn't terminate and does not give a correct answer of a factorial.

Related questions

+10 votes
1 answer
posted 5 years ago in Other by Danny William (520 points)
0 votes
0 answers
posted 2 years ago in Python by ladayton69 (7,080 points)
+10 votes
1 answer
posted 5 years ago in HTML by Milad1991 (2,750 points)
+18 votes
1 answer
posted 5 years ago in PHP by Matthew Tembo (6,610 points)