I’m trying to find multiple character in a string for this program. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I’m trying to find multiple character in a string for this program.

Everything outputs “Unique”. Something is wrong with the beginning of the if statement in the for loop but I don’t know what it is. I need to check if any character repeats in the string. https://code.sololearn.com/c5o4E80IOOab/?ref=app

7th Oct 2023, 11:34 PM
Josh Rhodes
12 Answers
+ 3
the problem with your code is that your using the count() function on a single character instead of the entire string. Fixed: 1. change the line: 'if i.count(i) > 1:' to 'if string.count(i) > 1:' 2. Remove 'else:' and make the second return statement match the level of indentation as the line 'for i in string' Like this: string = input() def repeats(string): for i in string: if string.count(i) > 1: return "Deja Vu" return "Unique" print(repeats(string))
8th Oct 2023, 1:27 AM
𝓭đ“Șđ“żđ’Ÿđ“­
𝓭đ“Șđ“żđ’Ÿđ“­ - avatar
+ 2
Wong Hei Ming, I understand your perspective, and I too believe in teaching rather than just handing out answers. However, in this specific case, I felt that the individual was so close to the solution that providing the tweaked code would be more beneficial than mere explanations. By seeing the exact changes in both explanation and the code, they can better understand the small nuances they missed without being confused of how to properly include the changes I addressed.
8th Oct 2023, 2:03 AM
𝓭đ“Șđ“żđ’Ÿđ“­
𝓭đ“Șđ“żđ’Ÿđ“­ - avatar
+ 2
Josh Rhodes your codebit url should be: https://code.sololearn.com/c5o4E80IOOab/?ref=app Pythosapien is right. It's probably a misunderstanding on how count() should be used. here is another one: (not just a free solution but as a sample code to google search for and perhaps learn more about Python's other methods and features) ------------------------------------- if len(letters:=[l for l in input()])>len(set(letters)): print("Deja Vu") else: print("Unique") ------------------------------------- google keywords "Python" "walrus operator" "list comprehension" "len" "set" or the one-liner: ------------------------------------- print((lambda x: "Deja Vu" if any(x.count(i)>1 for i in x) else "Unique")(input())) ------------------------------------- google keywords "Python" "lambda expression" "immediately invoked function expression" "IIFE" "any" Python is a versatile langauage. Trying to decipher other people's code and looking up explanations had always been a good learning method for me.
9th Oct 2023, 3:56 AM
Bob_Li
Bob_Li - avatar
+ 1
I don't know why I can't open your code in apps but opens on pc. Follow below instructions to insert code, or press the + sign > insert code, search and select your code and press done. https://code.sololearn.com/Wek0V1MyIR2r/?ref=app First of all, when running into error please take a look at the error message. It say File "./Playground/file0.py", line 7 if i.count(i) > 1: TabError: inconsistent use of tabs and spaces in indentation While it not always pointing where the error is, this message is close enough. At line 7 the indentation is not correct. Second, i.count(i) > 1 always return False. Correct use: givenWord.count(searchCharacter(s))
8th Oct 2023, 1:35 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Pythosapien, there was a old saying (also make a apperance in PC game Civilization 4) "Give a man a fish and you feed him for a day. Teach a man how to fish and you feed him for a lifetime." Providing a solution you only help other pass the exercise, but they often learn little from it. Give them a pointer, show them where the problem lies, provide them an example code (not solution code) and let them to figure out the problem. Learning from mistake is better than feeding a solution.
8th Oct 2023, 1:52 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Pythosapien, I know the feel when seeing someone is so close to the solution and want to help them nicely. However I also do notice the individual asked a few code coach questions these day, and the code is "so close" but shows sign of misconecption. Here are the example. https://www.sololearn.com/discuss/3245463/?ref=app https://www.sololearn.com/discuss/3245263/?ref=app https://www.sololearn.com/discuss/3245261/?ref=app I feel the individual needs guideline and practice, not the solution.
8th Oct 2023, 2:39 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Wong Hei Ming The way you keep telling me to insert my code is the way I’ve been doing it. So I’m not sure what else you want me to do. Also, I appreciate the help Pythosapien. I’m VERY new to programming. 2 months ago I didn’t even know what Python was so yeah there’s going to be a lot of misconception lol. I’m still very much learning.
8th Oct 2023, 4:57 AM
Josh Rhodes
+ 1
Wong Hei Ming you are right, teaching someone to fish is better than just handing over the fish. But what that person do with the fish is also important. If he just eats it and move on, then it's his loss. If he uses it to catch a bigger fish, then maybe it's just what he needs to move on to bigger and better things. Everything can be viewed as a goal or a starting point.😁 The second option is often the more worthwhile.
10th Oct 2023, 2:17 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li, I do agree a second option has to be considered. But looking at OP's profile it looks like the individual start to learn coding recently, and whom also confirmed my theory by saying "VERY new to programming 2 months ago." I think you also know a Chinese term (in English) "teach students in accordance with their aptitude." If I believe OP is experienced in coding but somehow stuck on a small problem in a bigger project, it is worthwhile to give a clear and visible code to help him out. If OP is a newbie, get the basic right is the most important thing to do. Just like you already mentioned, if OP is new to fishing and want to catch a big fish by asking for a small fish, giving out a small fish is not the best option. The one need to master the fishing skill first, otherwise the small fish will be wasted.
10th Oct 2023, 9:38 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Wong Hei Ming you are a far more conscientious teacher than those I have learned from 😁.
10th Oct 2023, 2:39 PM
Bob_Li
Bob_Li - avatar
+ 1
Wong Hei Ming What you’re saying is true for some but not so true for others. Everyone learns differently. I personally require small nudges in the right direction when I’ve been stuck for a while. I wouldn’t have asked for help if I hadn’t already spent a considerable amount time trying to solve the problem myself. In this case, 99% of my code was correct. It was literally one word on a single line that needed to be tweaked to make my code work.
10th Oct 2023, 3:04 PM
Josh Rhodes
0
Josh Rhodes, your original code only have 2 mistake. 1) indentation; 2) i.count(i) > 1 The first problem is easy to spot because the error message pointed to it, and I guess you knew it if looking at the error message. However the second one is not easy to spot and it caught on you. And as Pythosapien said, you are really close to it and deserve a helping hand. Consider your skill I made a hint in one line: Correct use: givenWord.count(searchCharacter(s)) I think it is obvious enough for you to spot the difference and pass the challenge. Also by knowing your mistake make you learn better. Yesterday I encounter another individual keep begging for answer in order to keep the streaks because time is running out. I think the thread is still on "Hot today" and have 50+ answers. For the first time I saw 2 mentors join the discussion and keep giving comments still the individual keep begging. I keep asking myself "Was my comments too harsh? " You can take a look and think about it.
10th Oct 2023, 3:48 PM
Wong Hei Ming
Wong Hei Ming - avatar