My program so far
def RemoveDuplicates(text):
result=[]
used=set()
for char in text:
if char not in used:
used.add(char)
result.append(char)
return ''.join(result)
text=input('Enter some text: ')
print(join(result))
I'm a beginner to Python so this is a quite difficult task for me. I know this doesn't add up, but where have I made the mistake?
-
1It doesn't look like you're ever calling RemoveDuplicates in your sample code.Moishe Lettvin– Moishe Lettvin2012年03月08日 22:03:51 +00:00Commented Mar 8, 2012 at 22:03
-
Well, Python doesn't have any magic. You can't write random stuff and hope it will work.Oleh Prypin– Oleh Prypin2012年03月08日 22:04:51 +00:00Commented Mar 8, 2012 at 22:04
5 Answers 5
You've never actually called your RemoveDuplicates()
function.
Replace:
text=input('Enter some text: ')
print(join(result))
with:
print(RemoveDuplicates(input("Enter some text:" )))
2 Comments
In addition to the other answers: if you're using Python 2, you need to use raw_input
instead of input
.
Comments
A few things here:
You defined the function
RemoveDuplicates
, but never actually call it.You use a
set()
, but then check for duplicates manually. A set, by definition, will get rid of duplicates automatically.You haven't told us what you are actually trying to accomplish. Do that, then we can help you more.
5 Comments
set
is being used manually because it's necessary to retain the order of the original text.if char not in used:
test is still redundant, which is the point @jwd was tring to make.used.add(char)
, but it is still needed for the result.append(char)
. I was misunderstanding at first.Your function looks okay to me, although I'm no expert. I think the problem lies in the fact that you're not actually calling it anywhere. Try calling it like this: print(RemoveDuplicates(text))
.
Comments
One small problem is that you forgot to call your function!
Additionally, if you're using Python 2.x the user input needs to be collected using raw_input
.
Two small fixes and your code works fine:
def RemoveDuplicates(text):
result=[]
used=set()
for char in text:
if char not in used:
used.add(char)
result.append(char)
return ''.join(result)
text = raw_input('Enter some text: ')
print(RemoveDuplicates(text))
1 Comment
input
for python 3.x.