Help with a python code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

Help with a python code

this supposed to check if there's Russian characters in a string but keeps giving no bad letters https://code.sololearn.com/cJ7JKr7C5WL4/?ref=app

15th Aug 2018, 7:01 PM
LONGTIE👔
LONGTIE👔 - avatar
16 Answers
+ 11
Just A Rather Ridiculously Long Username LONGTIE 's code doesn't work because he didn't change it the way I suggested. It's still self-referencing. Here you go: #this is not needed import sys, codecs sys.stdout = codecs.getwriter('utf_16')(sys.stdout.buffer, 'strict') bad_char = ['а', 'е', 'с', 'М', 'О', 'С', 'А', 'В', 'Е', 'Р', 'Х', 'К'] msg = 'аК' # copied from bad_char for char in bad_char: print(char) if char in msg: print("bad letter found") else: print("no bad letters") The output will be: a 'bad letter found' (every other letter of the list, followed by 'no bad letters') К 'bad letter found'
15th Aug 2018, 9:48 PM
Anna
Anna - avatar
+ 9
It's not a problem with encoding, but with your loops. You're creating kind of a self-referencing cycle loop... Do it like this and it should work: for char in bad_char: if char in msg: print("bad letter found") else: print("no bad letters")
15th Aug 2018, 8:42 PM
Anna
Anna - avatar
15th Aug 2018, 7:50 PM
LONGTIE👔
LONGTIE👔 - avatar
15th Aug 2018, 7:57 PM
LONGTIE👔
LONGTIE👔 - avatar
+ 7
_aleksandr_g no I'm trying for the contest
15th Aug 2018, 7:20 PM
LONGTIE👔
LONGTIE👔 - avatar
15th Aug 2018, 7:29 PM
LONGTIE👔
LONGTIE👔 - avatar
+ 6
Hello!Here you go. Are you from Russia? https://code.sololearn.com/c9lcOO8smL7S/?ref=app
15th Aug 2018, 7:19 PM
_aleksandr_g
_aleksandr_g - avatar
+ 5
It seems that it is a problem with sololearn's encoding of strings. Try this in IDLE: https://code.sololearn.com/cC1T8B6ffQiy/#py
15th Aug 2018, 7:47 PM
Just A Rather Ridiculously Long Username
+ 4
Nothing will print in the code playground. Paste it into IDLE on your pc or into a new .py file.
15th Aug 2018, 7:56 PM
Just A Rather Ridiculously Long Username
+ 4
No problem :)
15th Aug 2018, 7:58 PM
Just A Rather Ridiculously Long Username
+ 3
Sorry, but that's not the answer, it just prints "bad letter found" for everything
15th Aug 2018, 7:27 PM
Just A Rather Ridiculously Long Username
+ 3
@Anna Actually, there is a problem with the encoding. Your solution works no better than mine in code playground.
15th Aug 2018, 9:20 PM
Just A Rather Ridiculously Long Username
+ 3
first off... just set the file encoding like it suggests in PEP 263 https://www.python.org/dev/peps/pep-0263/ using sys is the WRONG way to set a file encoding. secondly, an optimisation. put all the bad chars into a single string: bad_chars = 'abcdefg' for char in string: if char in bad_chars: print('bad char') else: print('good char') or in a one liner: [print('bad char') if char in bad_chars else print('good char') for char in string]
16th Aug 2018, 7:04 AM
Aidan Haddon-Wright
Aidan Haddon-Wright - avatar
+ 2
@Anna I get what you're saying, and in fact my code would work exactly the same if you directly assign variables the values we're looking for. But in code playground, the input() function takes in input in some different sort of encoding, as suggested by the question marks instead of non-printable characters typed into input(). Which is why you can't compare the input with the characters in the array. ------------------------------- "LONGTIE 's code doesn't work because he didn't change it the way I suggested. It's still self-referencing." Even if it wasn't self referencing, it wouldn't work in code playground as long as input() is used to enter the values to be checked. ----------------------------- I have changed my example to call the same function for the two, so the difference would be clear. https://code.sololearn.com/cC1T8B6ffQiy/#py You can change your own code to assign 'msg' to an input() to see the difference.
15th Aug 2018, 10:51 PM
Just A Rather Ridiculously Long Username
+ 2
LONGTIE check this code I modify your code it works and your code contain many lines and i cover it in few lines hope it help you and good luck for challenge. https://code.sololearn.com/cs4VKWTKgSAP/?ref=app
16th Aug 2018, 4:51 AM
Maninder $ingh
Maninder $ingh - avatar
- 1
suggest me python project
16th Aug 2018, 4:57 AM
Bhavi Dhingra
Bhavi Dhingra - avatar