0

What's wrong with this code ?

letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] symbols = ['!', '@', '#', '

#x27;, '%', '&', '*'] num = ["1","2","3","4","5","6","7","8","9","0"] word = input() def split(kalima) : return list(kalima); let = split(word) def common_data(list1, list2): result = False for x in list1: for y in list2: if x == y: result = True return result return result def common_nums(list1 , list2) : for a in list1 : for c in list2 : if a == c : amf1 = 'good' for b in list1 : for d in list2 : if b == c : amf2 = 'good' if amf1 == amf2 : res = True return res return res if common_data(let , letters ) == True and common_nums(let , symbols ) == True and common_nums(let , num ) == True and len(let)>6: print("Strong") else : print("Weak")

22nd Jul 2021, 4:00 PM
Anas Kebir
9 Antworten
+ 1
1- First of all, you can shorten your code wigh a big one : why don't you try the string module: from string import ascii_letters,digits,punctuation 2- Secondly: I don't like a function overwriting like the split() redefining.Just use a list compression like : a = [i for i in 'blablabla']. 3- Third on the go : at common_data() you can just return True or False without using any plus variable: for ...: for ...: if x == y: return True else: return False 4- Four in a row : in common_nums() you actually have no variable called 'amf1' or 'amf2' in function scope, they are only in their 'if' branch respectively.Tha will cause an BIG ERROR !!! So try to take ... for example a tuple: res = (None,None) # for storing two results then compare : if res[0] == res[1] again without additional variable. 5- Lastly : Nice try ! I like your thought. Fix it and share here to see your improvements 😄
22nd Jul 2021, 9:48 PM
Ervis Meta
Ervis Meta - avatar
0
Thanks so much !! But i didn't understand the 4th advice !!
24th Jul 2021, 11:39 AM
Anas Kebir
0
Let me explain it again : if you know what 'variable scope' means, then you should see that the scope of amf1 and amf2 stands only inside the if branches , and not at main scope of the function , so they can't be used iutside of if branches. That's why it will raise an error 😁
24th Jul 2021, 11:44 AM
Ervis Meta
Ervis Meta - avatar
0
I get it , but how can I fix it?
24th Jul 2021, 11:45 AM
Anas Kebir
0
Just declare a variable, like a tuple, to store the results : result = ('neutral','neutral') Then just change it's elements like : if ... : result[0] = 'good' else: result[1] = 'bad'
24th Jul 2021, 11:49 AM
Ervis Meta
Ervis Meta - avatar
0
from string import ascii_letters,digits,punctuation letters = [i for i in ascii_letters ] symbols = [i for i in punctuation ] num = [i for i in digits ] word = input() let = list(word) def common_data(list1, list2): result = False for x in list1: for y in list2: if x == y: return True return True def common_nums(list1 , list2) : amf2 = ['neuutral','neutral'] amf1 = ['neutral','neutral'] for a in list1 : for c in list2 : if a == c : amf1[0] = 'good' else : amf1[1] = 'bad' for b in list1 : for d in list2 : if b == c : amf2[0] = 'good' else : amf2[1] = 'bad' if amf1 == amf2 : return True return True if common_data(let , letters ) == True and common_nums(let , symbols ) == True and common_nums(let , num ) == True and
24th Jul 2021, 12:09 PM
Anas Kebir
0
How about this ?
24th Jul 2021, 12:09 PM
Anas Kebir
0
Now is better 😁 Don't forget to see for improvements
24th Jul 2021, 5:17 PM
Ervis Meta
Ervis Meta - avatar
0
Thanks ☺️
25th Jul 2021, 11:33 AM
Anas Kebir