Factorial 5/15 help


#1

Hi,

i dont really seem to get this one, here is the solution:
def factorial(x):
total = 1
while x > 0:
total *= x
x -= 1
return total

print(factorial(1))

what is the point of x -=1 ?


#2

To give us this…

x * (x - 1) * (x - 2) * (x - 3) * .. * (x - x + 1)

as in,

7 * 6 * 5 * 4 * 3 * 2 * 1

which is 7! (7 factorial)


#3

thanks, that makes sense.

on this one:
def censor(text,word):
words = text.split()
result = “”
stars = “*” * len(word)
count = 0
for x in words:
if x == word:
words[count] = stars
count += 1
result = " ".join(words)
return result

print(censor(“this hack is wack hack”, “hack”))

what is the point of the count += 1?


#4

In the above, count is used to track the index of each word as it is reached in the for loop. It’s not the way an experienced programmer would write it, but as a naive approach it gets the job done and does not change the length of the list of words, only mutate those that need to be censored.

>>> words = "ValueError: invalid literal for int() with base 10: '0FF0FF'".split()
>>> for i, x in enumerate(words):
	print (i, x)

	
0 ValueError:
1 invalid
2 literal
3 for
4 int()
5 with
6 base
7 10:
8 '0FF0FF'
>>>