I tried to create a function for generating a set number (numbersToChoose) of values between two other values (startFrom and stopAt) but for some reason the iterator (?) in the second for-loop (line 7), in this case a seems to be randomly generated even though I don't see any reason for that.
def chooseRandomNumbers(numbersToChoose, startFrom, stopAt): numbers = [(randrange(startFrom, stopAt))] # initialize empty list of numbers for n in range(numbersToChoose-1): # choose random number from 1 to 49 a total of 7 times x = randrange(startFrom, stopAt+1) for a in numbers: # check for already existing elements in "numbers" if x == numbers[a]: # if new random number already exists in "numbers" discard and run loop again n -= 1 # decreases n by 1 so a new (hopefully not already existing) number can be generated else: # generated numbers does not exist in "numbers" yet numbers.append(x) # appends randomly chosen number to list "numbers" return numbers # returns said list "numbers"
Any advice on how to deal with this is greatly appreciated. Also please tell me if possible anything else bad in the code (just started python).