Factorial, need guidance


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/5?curriculum_id=4f89dab3d788890003000096


Oops, try again. factorial(2) returned None, did you remember to return the result?


Now I haven't gotten to my final product yet, I'm focusing on getting result2 working, and I cannot. My idea is that result2 will print out the numbers 1, 2, 3 and so on depending on what I have wrote in. I don't know if I can multiply them afterwards, but that's a problem for later, what I want to figure out is why result2 is not adding up like I'm hoping it should

def factorial(x):
    z = int(x)
    result1 = 1
    result2 = 0
    num = x 
    if x == 1:
        print result1 
        return result1
    elif x > 1:
        while result2 >= num:
            print result2
            result2 += 1
    
factorial(2)
# Line for testing


#2

this is your while loop:

while result2 >= num:

for x=2 (your test line), the first run of the loop would be the following:

while 0 >= 2:

this is false, so your loop never runs

a loop runs for as long as the condition is True


#3

Ah, thank you. Also, another question, is there any way of writing "For every time result2 prints, do this", if you get what I mean? Sorry if it sounds vague.

EDIT: Actually, doesn't matter.


#4

yea, just put the code in the while loop (same indention level as print), any further guidance needed?


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.