Question
Python code
You will need to make use of the random library for this homework. It is pretty simple to use. You can use the following methods: random.choice(list) returns a single item from the list, tuple or string at random. random.sample(list.o - returns a new list of n items randomly chosen from the list, tuple or string random shufflellist)randomly reorders all of the items in the list and changes the list itself. The list must be a mutable object so this will not work with strings or tuples random.randintim.a) -returns a randomly selected number between m and n as long as mcen. m and n are included in the possible return values. There are other methods that you can look at on your own but these should be all you will need for this assignment. A complete sample program might be: import random or: from random import shoice samal& sbutfle candint if you want to avoid using random. each time print(random.choice(elements)) print(gandomsample(elements,2)) random.sbuttlelfelements) print(elements) print(candom.tandint(1,5)) print(choice(elements)) print(sample(elements,2) shuffle(elements) trying to use print(random shuffle(elements)) results in None because shuffle doesnt return anything print(candint(1,5) The output in either case might be: water

media%2F904%2F904ee371-c9a3-42c7-a609-bd
0 0
Add a comment Improve this question Transcribed image text
Answer #1

for question 1 used random module to generate random numbers for height,width,length

import random
def calc(length,width,height=1):
    # if height == 1 then only multiplying length and width
    # else multiplying all
    return (length*width,"sq ft") if height == 1 else (length*width*height,"cu ft")

def main():
    # using random generating random number
    length = random.randint(1,30)
    width = random.randint(1,30)
    height = random.randint(1,30)
    # calling the functions with diff param
    print(calc(length,width))
    print(calc(length,width,height))

if __name__ == '__main__':
    main()

# end of script

#OUTPUT your outmay diifer as it is random number

(54, 'sq ft')
(378, 'cu ft')

Please do let me know if u have any concern...

Add a comment
Know the answer?
Add Answer to:
Python code You will need to make use of the random library for this homework. It...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • urgent help with python. can somone code this please and show output QUESTION: There are a variety of games possi...

    urgent help with python. can somone code this please and show output QUESTION: There are a variety of games possible with a deck of cards. A deck of cards has four different suits, i.e. spades (*), clubs (*), diamonds (), hearts (), and thirteen values for each suit. Now write a function that uses list comprehension to create a deck of cards. Each element in the list will be a card, which is represented by a list containing the suit...

  • Questions 1. How to create a comment in python? 2. The way to obtain user input...

    Questions 1. How to create a comment in python? 2. The way to obtain user input from command line 3. List standard mathematical operators in python and explain them 4. List comparison operators in python 5. Explain while loop. Give example 6. Explain for loop. Give example 7. How to create infinite loop? And how to stop it? 8. Explain a built-in function ‘range’ for ‘for’ loop 9. Explain break statement 10. Explain continue statement 11. Explain pass statement 12....

  • How to write python code that is a dice rolling function that generates random numbers. The...

    How to write python code that is a dice rolling function that generates random numbers. The dice rolling function takes two arguments: the first argument is the number of sides on the dice and the second argument is the number of dice. The function returns the sum of the random dice rolls. For example, if I call roll dice(6,2) it might return 7, which is the sum of randomly chosen numbers 4 and 3. It somewhere along the lines of:...

  • given code: assignment: use python to extend the given code to create an output similar to...

    given code: assignment: use python to extend the given code to create an output similar to the one in the black box please explain the final code import random HALMst N in = int(input('Enter Total Number of Cards: ") NM in NOON A = [] B [] E - [] D = [] E = [] for i in range(1, n+1): A.append(i) D.append(i) E.append(i) for j in range(n): 8.append(random.choice(D)) D.remove(B[5]) C.append(random.choice(E)) E.remove(C[j]) c= 0 for k in range(n): if (Brkl...cikl):...

  • Please use python. You can import only: from typing import Dict, List from PIL import Image...

    Please use python. You can import only: from typing import Dict, List from PIL import Image import random Questions are: --------------------------------------------------------------------------------------------------------------------------------- 1. def rotate_picture_90_left(img: Image) -> Image: """Return a NEW picture that is the given Image img rotated 90 degrees to the left. Hints: - create a new blank image that has reverse width and height - reverse the coordinates of each pixel in the original picture, img, and put it into the new picture """ -----------------------------------------------------------------------------------------------------------------------------------    2. def...

  • Using python The following is a modified version of the magic 8 ball. We have 20...

    Using python The following is a modified version of the magic 8 ball. We have 20 mysterious answers to questions about anything in the universe. Each time we start this program by randomly picking 8 answers and then we randomly pick 1 from these 8 answers. This lucky answer will then be printed. This time we use the sample() function from the random library. This function randomly samples a list and return the random samples in a list. import random...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • Use Python: Dice Rolls Create a function named build_roll_permutations which returns all 36 permutations of rolling...

    Use Python: Dice Rolls Create a function named build_roll_permutations which returns all 36 permutations of rolling two six sided dice. That is, each dice has a number 1 through 6 on one of its sides. The return value is a list which contains tuples. Each tuple represents a possible dice roll of two dice. Card Deck Create a function named build_deck that returns a full deck of cards. The cards are created by a rank and a suit (e.g. 2♡)....

  • !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random...

    !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...

  • use PYTHON Assignment 4 O Let's make random IDs. Get the number of IDs from the...

    use PYTHON Assignment 4 O Let's make random IDs. Get the number of IDs from the user to make from the keyboard. Then print the IDs. O Note that the IDs should be unique. Moreover, one ID is 11-length alphabet characters including digits (i.e., A-Z, A-Z, 0-9). Template Code Example import string import random num_of_ids = int(input("Enter the number of IDs: ")) # this list contains a-z, A-Z, and digits alphabet_digit_list = string.ascii_letters + string.digits Enter the number of IDs:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT