-2

have 2 functions. first one generates a list and the second one checks if theres duplicates. if theres duplicates it returns True

so i want to call function 2 from function 1 and if it returns true do something heres my code

import random

def x(list):
    for i in range(len(list)):
        count = 0
        for k in range(len(list)):
            if list[i] == list[k]:
                count += 1
            if count > 1:
                return True
    if count == 1:
        return False



def generated_list(N):
    list = []
    for i in range(N):
        list.append(random.randint(1, 365))
    x(list)


if generated_list(25) is True:
   print('is true')
  • 3
    1. `generated_list` does not return anything 2. `list` is a very bad variable name 3. See https://stackoverflow.com/questions/1541797/how-do-i-check-if-there-are-duplicates-in-a-flat-list/1541827 – DeepSpace Apr 23 '20 at 17:41
  • 1
    `return x(list)`‽ – deceze Apr 23 '20 at 17:43
  • By the way, if you want to generate a random yet unique list, you might want to look into a set instead of a list. And in generated_list, you want to return (x(list) – Sri Apr 23 '20 at 17:43

1 Answers1

1

There were some logical errors, check this one :

import random


def check_duplicate(numbers):
    for i in range(len(numbers)):
        count = 0
        for k in range(len(numbers)):
            if i == k:
                continue
            if numbers[i] == numbers[k]:
                count += 1
            if count > 1:
                return True
    return False


def generated_list(n):
    numbers = []
    for i in range(n):
        numbers.append(random.randint(1, 365))
    return check_duplicate(numbers)


if generated_list(25) is True:
    print('is true')

Also, avoid reserved keyword for naming your variables.

Taohidul Islam
  • 5,246
  • 3
  • 26
  • 39