Homework 6 - More with Lists
Due Date: November 3, 2023
Problems
How random is the
randint
function of therandom
module? Let's find out! Write a function that takes one integer parameter, say \(N\), and generates \(N\) random numbers \(x_1,x_2,\ldots,x_N\) where each number \(x_i\) is either \(0\) or \(1\), and returns the percentage of the numbers that were equal to \(1\). Call your functionmy_experiment
. Call your function several times with some choice for \(N\); choose at least \(N\geq 1000\). Are the results of your experiment surprising? Explain. You can load therandint
function using the following code:from random import randint
- What does the built-in function
isinstance
do? Give examples that use a list, tuple, integer, float, and string data types. Write a function called
find_indices
that has two parameters, saya
andB
whereB
is supposed to be a list, and returns a list of the indices whereB
has value equal toa
. Your function should check that the parameterB
is a list object (use theisinstance
function) and if it is not then an error message is returned. Ifa
is not in the listB
then your function should returnFalse
. For example, here are some outputs offind_indices
:>>> find_indices(3, [4, -1, 3, 4, 7, 5, 3]) [2, 6] >>> find_indices('yes', ['bob', 'joe', 'bob', 'two']) False >>> find_indices('rabbit', 'Once a upon a time') Error: The second argument is not a list
- See video instructions. Your game should include the following:
- The "turn" or "play" number should be displayed.
- Allow the game to end in a draw.
- Allow the user to continue playing after the end of a game.
- Display a message if a user enters a row/column that already has a symbol and allow the user to enter a new row/column.
- Your code should be thoroughly commented. All
if-elif-else
,for
loops,while
loops, etc. should be preceded by comments that describe what the forthcoming code is doing. Uncommented code will receive little credit.
This might be the most complicated program that you code so far. You can easily find solutions to this problem on the web, however, avoid the temptation to copy-paste a solution that you find. Try to this problem on your own; you will learn a lot if you do. Good luck!