In: Computer Science
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)
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