0
Can someone help me shorten this code. The output should allow to input number and tell where the duplicate number is indices
2 Antworten
+ 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
+ 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")