Caleb Richardson
Caleb Richardson

Reputation: 17

Python checking for number in input

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)

Upvotes: 1

Views: 106

Answers (2)

Rohit Jain
Rohit Jain

Reputation: 213411

You can build a set to just print unique numbers:

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

Upvotes: 3

Matt Bryant
Matt Bryant

Reputation: 4961

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.

Upvotes: 0

Related Questions