133k views
3 votes
(Print distinct numbers) Write a program that reads in integers separated by a space in one line and displays distinct numbers in their input order and separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). Hint: Read all the numbers and store them in list1. Create a new list list2. Add a number in list1 to list2. If the number is already in the list, ignore it.

User Gblazex
by
5.3k points

1 Answer

4 votes

Answer:

Following are the program to the given question:

num = input("Enter numbers: ")#defining a variable num for input value

list1 = num.split()#defining a list1 that adds value in list1

list2 = []#defining an empty list list2

for j in range(len(list1)):#defining a for loop that checks the list value in range

if not list1[j] in list2:# use if block that checks multiple value

list2.append(list1[j])#add value in list2

print("The distinct numbers are: ")#print message

for j in range(len(list2)):#defining for loop for print list value

print(list2[j])#print list value

Output:

Enter numbers: 1 22 55 7 22 1 1 2 3

The distinct numbers are:

1

22

55

7

2

3

Explanation:

In the above-code, a "num" variable is defined, that takes inputs from the console screen, and in the next step, "list1 and list2" is defined. In the "list1" it adds num variable value a into the list.

In the next step, a for loop is defined that checks the list value in range , and define an if block, that checks multiple value and add value into the list2, and in the next step, the for loop is defined that prints the list2 values.

User Ray Oei
by
5.3k points