torch.cuda.device_count() will give you the number of available devices, not a device number
range(n) will give you all the integers between 0 and n-1 (included). Which are all the valid device numbers.