[Tutor] For Loops Program Issue

Richard Damon Richard at Damon-Family.org
Tue Aug 18 12:13:59 EDT 2020


On 8/18/20 11:17 AM, eric grunfeld wrote:
> Good Morning Everyone:
>
> I am currently taking a beginners Python course that is being taught by
> Google, on the Coursera platform.
>
> I have been working on a problem but I seem to be obtaining an error
> message after running the program.
>
> Here is the problem:
>
> def factorial(n):
>     result = 1
>     for i in range(result,n):
>         result = result * n+1
>     return result
>
> print(factorial(4)) # should return 24
> print(factorial(5)) # should return 120:
>
> Here is the the error message that I have been receiving:
>
> Here is your output:
> 85
> 781
>
> Not quite. Pay attention at the operations that the code is
> doing and make sure that the factorial function returns the
> product of the numbers between 1 and n.
>
> Based upon my understanding, it appears that my issue is with the set-up of
> my range.
>
> I look forward to any thoughts or comments that you may have.
>
> All the best,
> Eric

Why is your range using 'result' as its base? What values do you WANT
out of the range? What values is range defined to give you?

look at the statement: result = result * n+1

Spaces matter for program nesting of control flow, it does not affect
the order of operations in a statement. What are you expecting here?

-- 
Richard Damon



More information about the Tutor mailing list