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)
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.
+ 6
RandomTuber ,
why do you think that len(...) is giving an issue?
+ 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
+ 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?
+ 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/
+ 1
NinjaGamer yeah
It because I am trying it that why it looking like that
+ 1
NinjaGamer thank u very much it worked
+ 1
PanicS thank u
+ 1
I think the error is around 'Len('
I'm not sure
+ 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)
0
If() => if()
Len() => len()
Print() => print()
0
Oyedeji Hiqmat :
w=0 đ
x="123f4"
for i in x:
if i in "abcdefghijklmnopqrstuvwxyz":
w=len(x.replace(i,""))
print(w)
0
Just declare random value of w before for loop its value will change