How come this code doesn't work for exercise 5 of part 5?


#1

https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/factorial?action=resume_content_item

n = raw_input("Enter an integer")

def factorial(x):
  	x = int(x)
  	total = 1
  	while x>0:
   		total *= x
    	x-=1
   	return total
  
factorial(n)

#2

you have an infinity loop, x is not decreased within the loop


#3

Hi @speed_reader_steve,

You should perform the conversion of the user’s input prior to calling the function.

Why are you not displaying the result of the function call?


#4

I believe you are providing to much code. The excersice asks to define a function nothing more.

Try this

def factorial(x):
    x = int(x)  #not sure if this line is even needed
    total = 1 
    while x > 0: 
        total *= x 
        x-=1 
    return total

#5

Why is x not decreasing? It’s part of the loop


#6

no, its not. here is a screenshot of your code:

image


#7

Still doesn’t work with this code. Copy/paste indents it in a way that isn’t consistent with what’s actually on the page. I recommend viewing the link too, thanks.

https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/factorial?action=resume_content_item

n = int(raw_input("Enter an integer"))

def factorial(x):
  total = 1
  while x>0:
    total *= x
    x-=1
  return total
  
factorial(n)

I wanted to see if I could mod the code to have a raw_input, but I’m wondering why it doesn’t work.


#8

what if you run your code on repl? Could be the exercise validation doesn’t like the use of raw_input.


#9

Okay well it seems to work now on that site, thanks.