+ 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
16 odpowiedzi
+ 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'
+ 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")
+ 8
Just A Rather Ridiculously Long Username
nothing prints
+ 8
ok thanks Just A Rather Ridiculously Long Username
+ 7
_aleksandr_g no I'm trying for the contest
+ 7
Just A Rather Ridiculously Long Username
do you have a idea?
+ 6
Hello!Here you go. Are you from Russia?
https://code.sololearn.com/c9lcOO8smL7S/?ref=app
+ 5
It seems that it is a problem with sololearn's encoding of strings. Try this in IDLE:
https://code.sololearn.com/cC1T8B6ffQiy/#py
+ 4
Nothing will print in the code playground. Paste it into IDLE on your pc or into a new .py file.
+ 4
No problem :)
+ 3
Sorry, but that's not the answer, it just prints "bad letter found" for everything
+ 3
@Anna Actually, there is a problem with the encoding. Your solution works no better than mine in code playground.
+ 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]
+ 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.
+ 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
- 1
suggest me python project