Question

In: Computer Science

Write a function name randintlist which takes three parameters: 1.minimum 2.maximum 3.length The randintlist function should...

Write a function name randintlist which takes three parameters:

1.minimum

2.maximum

3.length

The randintlist function should return a list of random int values (using random.randint) where the minimum and maximum values of those random numbers are the argument values for the first two parameters, and the length of the list(count of random integers ) is given by the argument value of the third parameter

For the list of random int values returned by randintlist no two values should be equal (e.g.,[1, 2, 1] is not valid, but [4, 1, 2] is valid)

Prompt the user whether they want to play a guessing game of easy, medium, or hard difficulty. Repeatedly re-prompt the user until their response is one of: easy, medium, hard

Using your randintlist function, generate a list of random int values between 1 and 10 (inclusive of both 1 and 10) whose length is:

1. 5, if user responded they wanted an easy game

2. 3, medium game

3. 1, hard game

Prompt the user to guess a number between 1 and 10. Repeatedly re-prompt the user if their response is not between 1 and 10 (don’t worry about user responses that are not valid int values)

If the user’s response (guess) is one of the values in the generated list of values (from randintlist function) then print ”You won” and if the user’s response (guess) is not one of the values print ”You lost” (capitalization matters in your output)

Solutions

Expert Solution

Python code pasted below.

import random
#function for random integer list generation
def randintlist(minimum,maximum,length):
     #initializing an ampty list to store random numbers
     randomlist=[]
     for i in range(length):
          #random numbers are generated and added to randomlist
          randomlist.append(random.randint(minimum,maximum))
     #For avoiding duplicate values, it is casted to set and stored again    
     randomlist=set(randomlist)
     #Converting back to list
     randomlist=list(randomlist)
     return randomlist
#main program
while True:
     #reading for easy,medium or hard
     game=input("Whether you want to play a guessing game,enter(easy,medium,hard):")
     if game=="easy":
          length=5
          while True:
               n=int(input("Enter a number between 1 and 10:"))
               if n>=1 and n<=10:
                    #generating a random integer list of length 5
                    numbers=randintlist(1,10,5)
                    print(numbers)
                    for item in numbers:
                         if item==n:
                              print("You won")
                              break
                    else:print("You lost")
                    break
          break
     elif game=="medium":
          length=3
          while True:
               n=int(input("Enter a number between 1 and 10:"))
               if n>=1 and n<=10:
                    #generating a random integer list of length 3
                    numbers=randintlist(1,10,3)
                    print(numbers)
                    for item in numbers:
                         if item==n:
                              print("You won")
                              break
                    else:print("You lost")
                   
                    break
          break
     elif game=="hard":
          length=1
          while True:
               n=int(input("Enter a number between 1 and 10:"))
               if n>=1 and n<=10:
                    #generating a random integer list of length 1
                    numbers=randintlist(1,10,1)
                    print(numbers)
                    for item in numbers:
                         if item==n:
                              print("You won")
                              break
                    else:print("You lost")
                    break
          break
     

Python Code in IDLE pasted for better understanding of the indent.

Output Screen - Test Case 1

Output Screen - Test Case 2

Output Screen - Test Case 3


Related Solutions

1-Find the relative maximum and minimum for the function, 1/3*x^3-9x+2 2-Determine the intervals on which the...
1-Find the relative maximum and minimum for the function, 1/3*x^3-9x+2 2-Determine the intervals on which the function f(x)=1+2x+8/x 3- Identify the critical number of f(x)=x^2-5x+6 on interval [-2,2] 4- Identify the intervals where the function, f(x)=2x^3-24x+2 5- ln(sin^2x) find f'(x)
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the...
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the matrices are same or 0 otherwise. Set appropriate parameters and return type if necessary.
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
Write an overloaded function of function area with 3 (float) parameters. This function should calculate and...
Write an overloaded function of function area with 3 (float) parameters. This function should calculate and print out the product of the 3 parameters.
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
Challenge 3 – Make 2D Array Write a function that takes 3 parameters and makes use...
Challenge 3 – Make 2D Array Write a function that takes 3 parameters and makes use of your prior two functions to create a 2D array filled with a default parameter. var twoD = Init2D(<width>, <height>, <fill val>); Challenge 4 – Random Integer in Range Write a function to return a random integer between a minimum value and maximum value. var ival = IntRandomRange(<min>, <max>); Challenge 5 – Random Int 2D Array Use your prior functions to provide a function...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the name parameter value is a name in the form LastName, FirstName(e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName(e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the...
Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
Write a C++ function which takes an int array and its size as parameters. It returns...
Write a C++ function which takes an int array and its size as parameters. It returns an int indicating how many multiples of 3 are contained in the array. For example, if the array contains {2, 6, 8} your function should return 1. If the array contains {3, 10, 5, 6} your function should return 2. Here is the function prototype: // int array[]: array to search // size: number of elements in the array int countMult(const int array[], int...
Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT