Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

n Q1: Write Python code to do the following 1. Use the function random.randrange () to pick and print an even number from the range

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

n Q1: Write Python code to do the following 1. Use the function random.randrange () to pick and print an even number from the range [-5,5] 2. Use the function random.randrange() to pick and print an odd number from the range [-5,5] 3. Use the function random.choice() to pick and print a single colors from the sentence colors = 'red blue green orange' In [ ]: 1 ### Q1 answer 2 3. Q2: Write Python code that plays the ['rock', 'paper','scissors'] games N times, and places the results in a list called game1. Show your result for N = 10. For example, the result for N=8 can be as follows: game1 ['scissors', 'scissors', 'rock', 'paper', 'scissors', 'rock', 'scissors'] show the different resulting lists when the game is played 5 times Hint: modify the following code N = 8 for i in range(1, N): item random.choice(['rock', 'paper','scissors']) print(item) - In [ ]: generate gamel list 1 ### Q2 answer 2 3 In [ ]: ### Q2 Answer for game played times Q3: Write Python code that keeps playing the ['rock', 'paper', 'scissors'] game until 'rock' is played 3 times. Print the outcomes of the game in a list called rps_game. Hint: Use a while True loop In [ ]: 1 ## Q3 Answer 2 3 import random 4 5 6 0 0 No NE 8 9 print(game) Q4: Recall that the random. seed (r) function initializes the random-number generator used in a Python program, so that the same "random" results is produced when the experiment is repeated. . Apply this function so that the folloiwng loop produces identical results. Try with random seeds 2.0 and 8.0 import random n = 3 ## program 1 print('No random seed initialization') for k in range(n): print(f'trial {k} outcome is {random.random()}') In [ ]: 1 ## Q4 Answer 2 3 import random 4 n = = 3 5 ## program 1 6 print('No random seed initialization') 7 for k in range(n): 8 print(f'trial {k} outcome is {random.random()}') 9 10 print(" With random seed initialization') 11 ## program 2 12 13 print(" With a different random seed initialization') 14 ## program 3 15 16 Q5: Use the randrange() method to generate a random non-empty list of positive integers of length less than or equal 5. An example of possible output: [0] [0, 1] [0, 1] [0, 1, 2, 3, 4, 5] 2] [0, 1, Hint: Use a range function with random end value, i. e. range (start, random end-value) In [ ]: 1 ## Q5 Answer 2. 3 import random 4 5 for i in range(5): 6 7 print(r) r = Q6: write a python function randlist(k,n) to generate a list of random-length lists of integer sequences (i.e. list of lists) each starts at zero. The function accepts two parameters, k : the number of lists to be generated, and n : the maximum length of the list. Two sampl results from calling the function randlist(3,10) are: [[0, 1, 2], [0, 1], [0, 1, 2, 3, 4, 5, 6, 7, 8]] [[0], [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3]] In [ ]: 1 ## Q6 Answer 2. 3 def randlist(k, n): 4 generates a group of k random but even-length li 5 6 7 8 return randomlists 9 Q7: Apply the function randlist() few times and observe the outputs In [ ]: 1 ## Q7 Answer 2 3 a = randlist(3, 10) 4 print(a) 5 6 b = randlist(3, 10) 7 print(b) q8: Define a new function called listcoinflips(n) to return a list called coins containing the outcomes of flipping a fair coin n times. In [ ]: 1 ## Q8 Answer 2. 3 def listcoinflips (n): returns a list containing the outcomes of flipping A oo No return coins 8 9 10 11 In [ ]: 1 ## Test function here 2 3 listcoinflips (4) Q9: Using the listcoinflips(n) function you created above, write Python code to show the outcome of performing k trials, each consisting of flipping a coin n time, for any integers k and n. For example, the output of your code for n = 4 and k = 5 should be: [ trial 1 : ['H', 'H', 'H', ] trial 2 : ['H', 'H', 'T', 'T'] trial 3 : ['t', 'T', 'T', 'H'] trial 4 : ['H', 'T', 'H', 'H'] trial 5 : ['H', 'H', 'T', 'H'] In [ ]: 1 ## Q9 Answer 2 k 5 3 n 4 4 for j in range(k): 5 print(f'trial {j} : !) Q10: Write a new function called HTcount(n) that counts the heads and tails resulting from applying the listcoinflips (n) function you created above. For example, calling the function HTcount(100) can give the following answer in k = 100 trials, Heads 59 and Tails = 41 In [ ]: ## Q10 Answer 2 3 def HTcount(n): 4 returns two values H: the number of heads and T: the number from flipping a fair coin n times" 8 9 10 return H, T In [ ]: 1 ## Q10 test 2 k = 100 3 heads, tails print(f'in k HT count(k) {k} trials, Heads = {heads} and Tails = {tails}')

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

More Books

Students also viewed these Databases questions

Question

3. What may be the goal of the team?

Answered: 1 week ago

Question

How do modern Dashboards differ from earlier implementations?

Answered: 1 week ago