1

I need to write a program that will check the numbers that a user enters. If the user enters a number more than once then it will skip over it and print out only numbers that the user entered once.

I was playing around with this:

def single_element():
numbers = []
numbers = input("Enter some numbers: ").split()
for i in numbers:
    if i in numbers:
       i + 1   #I was trying to find a way to skip over the number here. 
print(numbers)

2 Answers 2

3

You can build a set to just print unique numbers:

numbers = input("Enter some numbers: ").split()
print set(numbers)
Sign up to request clarification or add additional context in comments.

Comments

0

Use a set. They are iterables, like lists, and can easily be converted back and forth. However, sets do not contain duplicate values.

def single_element():
   numbers = list(set(input("Enter some numbers: ").split()))
   print(numbers)

In this function, you get the input numbers as a list and convert them to a set, which will remove duplicates, and then convert back to a list.

Note: sets are not guaranteed to keep the same order like lists.

Comments

Your Answer

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.