Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix TypeError: Population must be a sequence. #22

Merged
merged 2 commits into from
Apr 8, 2023
Merged

fix TypeError: Population must be a sequence. #22

merged 2 commits into from
Apr 8, 2023

Conversation

ppdms
Copy link
Contributor

@ppdms ppdms commented Mar 30, 2023

Hello!

Running this game on Python 3.11 gives me the error "TypeError: Population must be a sequence. For dicts or sets, use sorted(d).", as seen below:

% python3 game.py
pygame 2.3.0 (SDL 2.24.2, Python 3.11.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "/Users/basil/Developer/temp/SudokuGame/SudokuGame/game.py", line 17, in
board = sudoku_generator.generateBoard(difficulty)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/basil/Developer/temp/SudokuGame/SudokuGame/sudoku_generator.py", line 5, in generateBoard
boardValues = str(generators.random_sudoku(avg_rank=difficulty))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/dokusan/generators.py", line 11, in random_sudoku
sudoku = Sudoku(*_random_initial_cells(box_size), box_size=box_size)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/dokusan/generators.py", line 34, in _random_initial_cells
values = random.sample(all_values, k=size)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.2_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/random.py", line 439, in sample
raise TypeError("Population must be a sequence. "
TypeError: Population must be a sequence. For dicts or sets, use sorted(d).

The small changes in this pull request seem to fix it: after making them, it runs without errors.

@unmade
Copy link
Owner

unmade commented Apr 8, 2023

Hey, thanks for noticing and contributing!

Yes, indeed there was a change in python3.11:

Changed in version 3.11: The population must be a sequence. Automatic conversion of sets to lists is no longer supported.

See: https://docs.python.org/3/library/random.html#random.sample


We don't need sorting here, so better to convert to list instead

Copy link
Owner

@unmade unmade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see comments

Co-authored-by: Aleksei Maslakov <[email protected]>
Copy link
Contributor Author

@ppdms ppdms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

@ppdms
Copy link
Contributor Author

ppdms commented Apr 8, 2023

GitHub's interface is confusing me a little, do I have to do anything else or can you just merge it? With list() instead of sorted() like you said 👍🏼

@unmade
Copy link
Owner

unmade commented Apr 8, 2023

I can just merge it, no need to do anything else

Nevermind the failed pipelines, it is not related to your changes

@unmade unmade merged commit 4ded940 into unmade:master Apr 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants