-3
from random import *
day = list(range(1, 29))
day = day[3:29]
shuffleday = shuffle(day)
print(shuffleday)

The result is None. What's wrong?

Chris
  • 26,361
  • 5
  • 21
  • 42
  • 1
    Please do a search before posting a new question - this one [already has many answers](https://stackoverflow.com/search?q=%5Bpython%5D+shuffle+returns+none), e.g. [Why does random.shuffle return None?](https://stackoverflow.com/q/17649875/12299000) – kaya3 Sep 29 '22 at 06:36
  • 2
    What is your expected output? – Arifa Chan Sep 29 '22 at 06:58

7 Answers7

0

random.shuffle modifies day. It does not return anything.

This is per convention in Python, where functions which modify the arguments given to them return None.

If you wish to generate a new list shuffled, without modifying the original, you may wish to use random.sample.

from random import *

day = list(range(1, 29))
shuffleday = sample(day[3:29], len(day2))
print(shuffleday)

If you simply wish to get a random element from a list, you can use random.choice.

from random import *

day = list(range(1, 29))
randomday = choice(day[3:29])
print(randomday)
Chris
  • 26,361
  • 5
  • 21
  • 42
0

random.shuffle does not return anything. It modifies the input list.

import random

day=list(range(1,29))
print(day)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28]
day=day[3:29]
print(day)
# [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28]

shuffleday=random.shuffle(day)
print(shuffleday)
# None
print(day)
# [27, 9, 14, 15, 7, 17, 28, 10, 23, 21, 16, 12, 6, 11, 22, 25, 24, 20, 5, 19, 13, 4, 18, 8, 26]
0

The value of shuffleday is None because the shuffle function does not return a new list, it just changes the order of the elements in the list supplied as an argument.

Chris
  • 26,361
  • 5
  • 21
  • 42
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 03 '22 at 02:54
0

To get a random number from day list use choice instead of shuffle, it will return a value of random element from day[3:29]:

from random import *
day = list(range(1, 29))
day = day[3:29]
shuffleday = choice(day)
print(shuffleday)
0

This will return a value of random number. Choose one:

shuffleday = choice(range(3,29))

or

shuffleday = randint(3,29)

or

shuffleday = randrange(3,29)
-1

random.shuffle modifies the list in place, it doesn't return a value. so you can just replace your second last line with

shuffle(day)

then simply print shuffle without making your new variable.

Additionally I would say it's better practice to just import what you need from random, rather than doing a * import and cluttering your namespace (especially considering you are new to the language and unlikely to know the name of every function in the random library). In this case you can do:

from random import shuffle
Chris
  • 26,361
  • 5
  • 21
  • 42
Jesse
  • 73
  • 7
  • So what kind of random function returns a value? – jjangdoll Sep 29 '22 at 06:34
  • You could instead start by making your copy first (ie shuffleday = day.copy()) and then shuffle your new list. You would only need to do this if you need the original list later in the code, otherwise you should just shuffle your original list and save the memory. – Jesse Sep 29 '22 at 06:43
-1

in fact the shuffle function does not return anything, it shuffles the elements in place (in day for your case).

try print(day) instead

abdo raqi
  • 34
  • 4