Can someone help me shorten this code. The output should allow to input number and tell where the duplicate number is indices | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone help me shorten this code. The output should allow to input number and tell where the duplicate number is indices

https://code.sololearn.com/cJhPsN47FuRR/?ref=app

11th Mar 2023, 3:25 AM
Yorokobi💜
Yorokobi💜 - avatar
2 Answers
+ 3
Your if condition always gonna true that's why it printing out of bound len<5 or len > 5 in case of 5 only condition will fail and your elseif part will execute you can use for loop to avoid such a lengthy code
11th Mar 2023, 3:55 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Try something like this, if you use formatted string, you can easily use variables in your string: # input example: 1 2 3 4 4 num_list = input().split() no = 0 if len(num_list) != 5: print("Out of bound") else: no = 2 for i in range(len(num_list) - 1): for j in range(i + 1, len(num_list)): if num_list[i] == num_list[j]: # use formatted string print(f'Duplicate numbers {num_list[i]} in the indices {i} and {j}') no = 1 break if no == 2: print("There are no duplicate numbers")
11th Mar 2023, 12:31 PM
Paul
Paul - avatar