I am having trouble with all the exercises past lesson 4 in Practice Makes Perfect.
I am trying not to look at the answer. Here is my code.
def factorial(x): for n in str(x): n = int(n * (n - 1)) return n factorial(7)
I defined function name factorial and gave it argument int x. To my understanding from the last lesson you can not iterate an integer so I turned it to a string so I could use a for loop. Then I created a variable (n) and it turned n to int because you can’t do operations on a string. I’m getting all kinds of errors. Am I going in the right direction or am I totally out of the ball park. This is the error I am getting: Traceback (most recent call last):
File “python”, line 5, in
File “python”, line 3, in factorial
TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’