+ 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
12 Antworten
+ 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))
+ 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.
+ 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.
+ 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))
+ 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.
+ 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.
+ 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.
+ 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.
+ 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.
+ 1
Wong Hei Ming
you are a far more conscientious teacher than those I have learned from đ.
+ 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.
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.