What is the wrong with this it just print Deja vu | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the wrong with this it just print Deja vu

https://code.sololearn.com/cL2RPPYfO76v/?ref=app

23rd Aug 2022, 8:33 PM
Abdullah Essa
5 Answers
+ 3
Abdullah Essa an if statement requires a Boolean expression. This code creatively embeds a for loop, but it does not work. Python interprets it as a generator object, which is non-null, so it is always truthy. This shows you how Python sees the expression: print(type( (A.count(i) > 1 for i in A) )) Output: <class 'generator'> To solve this problem easily, remember that a set does not allow duplicate members. Create a set out of the input string. Then compare its length with the original string length. If the set is shorter, then you know that it removed duplicate characters and you have a deja vue situation. If they are equal they are unique.
23rd Aug 2022, 11:21 PM
Brian
Brian - avatar
+ 3
I think your code runs once. I don't know what will interpreted when you use list comprehension syntax in if. anyway, you can try like this: word = input() for letter in word: if word.count(letter) > 1: print("Deja Vu") else: print("Unique")
23rd Aug 2022, 8:50 PM
Elmira
+ 1
yeah that's it. you can add additional info in print, like how many times that letter is repeated or avoid printing for the same letter or...
23rd Aug 2022, 9:06 PM
Elmira
0
https://code.sololearn.com/cu591RrJ8fL7/?ref=app Elmira you mean like this. The the printed word will be repeated for every letter
23rd Aug 2022, 9:01 PM
Abdullah Essa
0
Here's my code, hope this helps: h = sorted(input()) l = [] for x in range(-1,len(h)-1): x+=1 l.append(h[x]) for i in range(-1, x-1): i+=1 if l[i-1] == l[x-1]: re = True else: re = False l.sort() if re is True: print("Deja Vu") else: print("Unique") Ok, so in my code, I sorted the user input, create a empty list, make a variable o and assign it to False,check if every letter is the same. If there is a duplicated letter, it should add "Yes" into the list. Otherwise, it adds "No" into the list. If there is atleast 1 yes in a list, the o variable will reassign to True. If o is True, it prints Deja Vu, if False, it prints Unique.
24th Oct 2022, 12:56 AM
Dragon RB
Dragon RB - avatar