107k views
4 votes
Write a program that reads in numbers separated by a space in one line and displays distinct numbers

User Ryan
by
4.1k points

1 Answer

2 votes

Answer:

Here is the Python program:

def distinct(list1):

list2 = []

for number in list1:

if number not in list2:

list2.append(number)

for number in list2:

print(number)

numbers =(input("Enter your numbers: ")).split()

print("The distinct numbers are: ")

distinct(numbers)

Step-by-step explanation:

The above program has a function named distinct that takes a list of numbers as parameter. The numbers are stored in list1. It then creates another list named list2. The for loop iterates through each number in list1 and if condition inside for loop check if that number in list1 is already in list2 or not. This is used to only add distinct numbers from list1 to list2. Then the second for loop iterates through each number in list2 and displays the distinct numbers in output.

To check the working of the above method, the numbers are input from user and stored in numbers. Then these numbers are split into a list. In the last distinct method is called by passing the numbers list to it in order to display the distinct numbers. The program along with its output is attached in a screenshot.

Write a program that reads in numbers separated by a space in one line and displays-example-1
User Shital
by
4.0k points