I am still fairly new the Python and have been doing random exercises on various websites. The most recent one involves replacing every letter (symbol, whitespace, etc) of a string with either “)” or “(” based on the number of times the specific letter appears in the string. If it only appears once, it is replaced by “(“. Multiple appearances are replaced by “)”. Capitalized letters are treated the same as lower letters by the exercise.
I want to get assistance as to why my code is not working:
def duplicate_encode(word): word = word.lower() for x in word: if word.count(x) > 1: word = word.replace(x, ')') else: word = word.replace(x, '(') return word print(duplicate_encode('jSkkZSj)rmng(PKqDBrOH')) #currently outputs ')))))))))))))()((()((' #the desired output '))))())()((((()((()(('
For some reason, it is counting Z as having more than 1, same with ). Hopefully someone can point me in the right direction. Thank you in advance!
submitted by /u/No_Opposite8868
[link] [comments]
r/learnpython I am still fairly new the Python and have been doing random exercises on various websites. The most recent one involves replacing every letter (symbol, whitespace, etc) of a string with either “)” or “(” based on the number of times the specific letter appears in the string. If it only appears once, it is replaced by “(“. Multiple appearances are replaced by “)”. Capitalized letters are treated the same as lower letters by the exercise. I want to get assistance as to why my code is not working: def duplicate_encode(word): word = word.lower() for x in word: if word.count(x) > 1: word = word.replace(x, ‘)’) else: word = word.replace(x, ‘(‘) return word print(duplicate_encode(‘jSkkZSj)rmng(PKqDBrOH’)) #currently outputs ‘)))))))))))))()((()((‘ #the desired output ‘))))())()((((()((()((‘ For some reason, it is counting Z as having more than 1, same with ). Hopefully someone can point me in the right direction. Thank you in advance! submitted by /u/No_Opposite8868 [link] [comments]
I am still fairly new the Python and have been doing random exercises on various websites. The most recent one involves replacing every letter (symbol, whitespace, etc) of a string with either “)” or “(” based on the number of times the specific letter appears in the string. If it only appears once, it is replaced by “(“. Multiple appearances are replaced by “)”. Capitalized letters are treated the same as lower letters by the exercise.
I want to get assistance as to why my code is not working:
def duplicate_encode(word): word = word.lower() for x in word: if word.count(x) > 1: word = word.replace(x, ')') else: word = word.replace(x, '(') return word print(duplicate_encode('jSkkZSj)rmng(PKqDBrOH')) #currently outputs ')))))))))))))()((()((' #the desired output '))))())()((((()((()(('
For some reason, it is counting Z as having more than 1, same with ). Hopefully someone can point me in the right direction. Thank you in advance!
submitted by /u/No_Opposite8868
[link] [comments]