Pls I tried this code ...but it saying name 'w' not defined even though it was | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Pls I tried this code ...but it saying name 'w' not defined even though it was

x="123f4" for i in x: If i in "abcdefghijklmnopqrstuvwxyz": w=Len(x.replace(i,"")) Print(w)

27th Dec 2022, 3:35 PM
Oyedeji Hiqmat
Oyedeji Hiqmat - avatar
13 Answers
+ 6
PanicS , Oyedeji Hiqmat , it is not recommended to use global variables. we should avoid it whenever it is possible. here is a recommended code: x="123f4" for i in x: w = '' if i in "abcdefghijklmnopqrstuvwxyz": w=len(x.replace(i,"")) print(w) >>> the error appears in the first iteration cycle, because 'i' has the value of *1*. since this is not in 'abc...xyz', variable *w* will not be created in the following line. so the print statement will fail.
27th Dec 2022, 7:17 PM
Lothar
Lothar - avatar
+ 6
RandomTuber , why do you think that len(...) is giving an issue?
28th Dec 2022, 4:38 PM
Lothar
Lothar - avatar
+ 3
Hi Per Bratthammar, Thanks so much for sharing your insight. I accept that the error has nothing to do with scope. You are correct that variable w has a global scope. I delved deeper into the code to understand the bug, and then I realised that in the first iteration of the 'for' loop, i is assigned value '1' and the condition for if statement is not fulfilled (there isn't any numeric character in the string inside if condition). As such, the statement inside the if block viz. `w=len(x.replace(i,""))` fails to get executed, and therefore the variable w is not yet initialized. However in the first iteration of the 'for' loop, the program asks for execution of `print(w)` statement, and this causes the error. Thanks once again Per Bratthammar for your valuable insight.. Cheers
29th Dec 2022, 1:49 PM
Sanjay Sahu
Sanjay Sahu - avatar
+ 2
Hi, Oyedeji Hiqmat ! I’m not shure what you are trying to do. You never change the string x, so if a character in x is found in the alphabetic string, it will always show the same result, even if all charaters in x are in the alphabetic string. The result will always be: len(x) - 1. So the question is: what do you really want to do?
27th Dec 2022, 4:59 PM
Per Bratthammar
Per Bratthammar - avatar
+ 2
# Hi, Sanjay Sahu # There is no such a thing as a if-statement namespace. If you create # a variable inside a if-statement, it become global, not local. for i in (0, 1): if not i: a = i # Create a. else: b = i # Create b. # Print global variables created inside the if-statement: print(f"{a = }") # Prints 0. print(f"{b = }") # Prints 1. # Read about Pyton scope and the LEGB-rule: https://realpython.com/JUMP_LINK__&&__python__&&__JUMP_LINK-scope-legb-rule/
29th Dec 2022, 12:39 PM
Per Bratthammar
Per Bratthammar - avatar
+ 1
NinjaGamer yeah It because I am trying it that why it looking like that
27th Dec 2022, 3:43 PM
Oyedeji Hiqmat
Oyedeji Hiqmat - avatar
+ 1
NinjaGamer thank u very much it worked
27th Dec 2022, 4:25 PM
Oyedeji Hiqmat
Oyedeji Hiqmat - avatar
+ 1
PanicS thank u
27th Dec 2022, 4:25 PM
Oyedeji Hiqmat
Oyedeji Hiqmat - avatar
+ 1
I think the error is around 'Len(' I'm not sure
28th Dec 2022, 1:04 PM
RandomTuber
RandomTuber - avatar
+ 1
It's because variable w was declared inside the if block, and hence its scope remains local [inside the if block only]. You need to initialise variable w at the beginning itself, viz make it a global variable. Alternately, add the global keyword before it if you are initialising it inside the if block. x="123f4" w=0 for i in x: if i in "abcdefghijklmnopqrstuvwxyz": w=len(x.replace(i,"")) print(w)
29th Dec 2022, 12:18 PM
Sanjay Sahu
Sanjay Sahu - avatar
0
If() => if() Len() => len() Print() => print()
27th Dec 2022, 3:41 PM
NinjaGamer
NinjaGamer - avatar
0
Oyedeji Hiqmat : w=0 👈 x="123f4" for i in x: if i in "abcdefghijklmnopqrstuvwxyz": w=len(x.replace(i,"")) print(w)
27th Dec 2022, 3:45 PM
NinjaGamer
NinjaGamer - avatar
0
Just declare random value of w before for loop its value will change
29th Dec 2022, 11:16 AM
Ashish Ranjan