1

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?

Moishe Lettvin
8,4811 gold badge28 silver badges41 bronze badges
asked Mar 8, 2012 at 22:00
2
  • 1
    It doesn't look like you're ever calling RemoveDuplicates in your sample code. Commented Mar 8, 2012 at 22:03
  • Well, Python doesn't have any magic. You can't write random stuff and hope it will work. Commented Mar 8, 2012 at 22:04

5 Answers 5

1

You've never actually called your RemoveDuplicates() function.

Replace:

text=input('Enter some text: ')
print(join(result))

with:

print(RemoveDuplicates(input("Enter some text:" )))
answered Mar 8, 2012 at 22:03

2 Comments

@user1248367: It could be written even in one line, but with worse performance. The function seems good.
Oh there are plenty of ways to write that shorter -- in one or two lines, in fact. But it's homework and what you have works, so I'd turn it in. :-)
1

In addition to the other answers: if you're using Python 2, you need to use raw_input instead of input.

answered Mar 8, 2012 at 22:06

Comments

0

A few things here:

  1. You defined the function RemoveDuplicates, but never actually call it.

  2. You use a set(), but then check for duplicates manually. A set, by definition, will get rid of duplicates automatically.

  3. You haven't told us what you are actually trying to accomplish. Do that, then we can help you more.

answered Mar 8, 2012 at 22:03

5 Comments

Although since he is trying to remove duplicate characters from a string, a set() would not maintain sort order
Sorry. I want the program to ask the user to enter some text. The output should be the text with duplicates removed. Text entered: "Stackoverflow", Output: Stackoverflw
I'm pretty sure the set is being used manually because it's necessary to retain the order of the original text.
I think the if char not in used: test is still redundant, which is the point @jwd was tring to make.
@Marius: Well, the test isn't needed in order to do used.add(char), but it is still needed for the result.append(char). I was misunderstanding at first.
0

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)).

answered Mar 8, 2012 at 22:04

Comments

0

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))
answered Mar 8, 2012 at 22:06

1 Comment

raw_input is python 2. It was renamed to input for python 3.x.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.