+ 1

....it keeps saying that variable sum got referenced before assignment...HOW!

x = int(input()) sum = 1 def factorial(x): for i in range(0, x+1): sum *= i return print(sum) factorial(x)

1st Oct 2023, 6:11 AM
Intermediate Depression
Intermediate Depression - avatar
17 Respostas
+ 5
Regardless to the code logic… sum should NOT be assigned as a variable name. sum() is a built-in Python method. You can assign sum as a variable, but you cannot use it as a function later on. The error you are facing is UnboundLocalError. In you code, before factorial function is defined, sum is a GLOBAL variable. And INSIDE a function, variable sum is a LOCAL variable. Unless you instruct the function it can interact with variable OUTSIDE the function with ā€œglobalā€ keyword, variable sum outside the function is independent to the same variable name inside a function. Search for ā€œpython scopeā€ for more information. w3schools has a better explanation than I do.
1st Oct 2023, 6:54 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 5
- `return print(sum)` #You don't need to return a print statement!`, - Variable `sum` is declared before the function but you call it inside the function(local variable). It should be declared inside the function. [This is where the error occurs] - `range(0,x+1)` starts from 0 and ends with x, but multiplication by 0 is always 0. Now, you know what to do. If you fixed it or have no clue on how to fix it, try this code and see the output: https://code.sololearn.com/cc5e7kCkZZ4m/?ref=app
1st Oct 2023, 6:54 AM
Dragon RB
Dragon RB - avatar
+ 3
Wong Hei Ming , You can also use the sum() function after the variable `sum` declared. But, it's still better to avoid picking function names to name a variable.
1st Oct 2023, 7:04 AM
Dragon RB
Dragon RB - avatar
+ 2
Dragon BB, after sum is declared as variable, it lost the function. https://code.sololearn.com/ckq49gEF75Km/?ref=app
1st Oct 2023, 7:15 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
Wong Hei Ming I also found that you could use the `global` keyword to fix this problem.. https://code.sololearn.com/cBRdmbzm8IXw/?ref=app
1st Oct 2023, 7:49 AM
Dragon RB
Dragon RB - avatar
+ 1
Wong Hei Ming Sorry! My bad. What I meant was something like: def a(n): sum = 1 pass print(sum([1,2,3]))
1st Oct 2023, 7:31 AM
Dragon RB
Dragon RB - avatar
+ 1
That is the "scope" concept, the problem OP is facing! I make a quick check on intermediate course, looks like it is not mentioned.
1st Oct 2023, 7:35 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
1st Oct 2023, 7:46 AM
Dragon RB
Dragon RB - avatar
+ 1
That was mentioned in my post above. And i feel my explanation is not clear enough so I suggest take a look at w3schools.
1st Oct 2023, 7:53 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Wong Hei Ming Oh wow i didn't know that sum is lost as a func if used as a variable.....it applies to all built in func i will assume.
1st Oct 2023, 3:05 PM
Intermediate Depression
Intermediate Depression - avatar
+ 1
Dragon RB Using global keyword....i didn't even know that is possible......where do i learn these stuff because i am already done with sololearn and it has non of that.
1st Oct 2023, 3:08 PM
Intermediate Depression
Intermediate Depression - avatar
+ 1
@Intermediate Depression You can access a global variable from a function, but you cannot change the value without further instruction. Example 1 x = 10 def func_1(): y = x + 1 return y print(func_1()) Output: 11 In the above example, func_1() can access variable x and use it to make calculation, and store the result to ANOTHER variable. Example 2 x = 10 def func_2(): x += 1 return x print(func_2()) Output: UnBoundLocalError In the above example, func_2() try to modify a global function. Normally it is not allow. Example 3 x = 10 print(x) # 10 def func_3(): global x # enable to modify x which is OUTSIDE the function x += 1 return x print(func_3()) # 11 print(x) # 11 Output: 10 11 11 In the above example, 10 is assigned to x before func_3 is declared. By allowing func_3 to manipulate the global variable x, x is being modified by the function, turning x into 11. The second print shows func_3 made changes to global variable x. The third print returns 11, once again confirming the change of value is caused by func_3.
1st Oct 2023, 3:17 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Woo...looks like mention some with @ only works on apps version, not on website...
1st Oct 2023, 3:20 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Intermediate Depression, you can search "Automate the boring stuff with python", a book written by Al Sweigart. He is kind enough to publish his book on the web so everyone can read it for free. SoloLearn only teach us the basic. When I want to archive something with python, I search "how to <do something> in python". Usually I look for results from w3schools, geeksforgeeks and realpython for easy understanding. Sometime I look for stackoverflow for advance concept and try to figure it out (not always success, but tried anyway).
1st Oct 2023, 3:31 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Wong Hei Ming Thank you so much for all the valuable information. You were a big help thank you so much.
2nd Oct 2023, 4:46 PM
Intermediate Depression
Intermediate Depression - avatar
+ 1
Intermediate Depression Next time, instead of copy-paste your code in your question description, send us a link to your code instead. Here's how: https://code.sololearn.com/Wek0V1MyIR2r/?ref=app
2nd Oct 2023, 4:52 PM
Dragon RB
Dragon RB - avatar
0
Wong Hei Ming Ty so much for explaining i really appreciate it! Sorry to bother you further but i thought that global variables can be accessible from anywhere while local are not so why wasn't i able to access my global valuable???. (I only have a general idea about local and global )
1st Oct 2023, 2:50 PM
Intermediate Depression
Intermediate Depression - avatar