Question

In: Computer Science

Write a Python function that will ask the user for an integer X bigger than 20...

Write a Python function that will ask the user for an integer X bigger than 20 and return a list "ls" of 10 distinct random integers taken between the interval 0, to X. None of the 10 elements of "ls" should be repeated.

Solutions

Expert Solution

As per your requirement, here is the Python code:

Rawcode:

#importing random module

import random

#function which generates the distinct random integers taken between the interval 0, to X

def randomList(X):

    #creating an empty list

    ls = []

    #loop which runs 10 times to generate 10 random numbers

    for i in range(0, 10):

        #loop which runs until the random number which is not there in list

        while(1):

            #generating random number between 0 to X

            n = random.randint(0, X)

            #if the generated number is already in list

            if n in ls:

                #then again generating the number

                n = random.randint(0, X)

            #if the generated number is not there in list

            else:

                #appending that number to the list

                ls.append(n)

                #and breaking the while loop

                break

    #return the list

    return ls

#asking user to enter the integer greater than 20

X = int(input("Enter integer greater than 20: "))

#checking if given value is less than 20

if(X < 20):

        #printing that please enter the integer greater than 20

        print("Please enter integer greater than 20.")

else:

    #calling function which generates 10 distinct random elements in given range

    print(randomList(X))

Here is the screenshot of source code:

Here is the screenshot of output:

Hope this helps you. Please feel free to ask if you still have any queries.

Do upvote. Thank you !


Related Solutions

Write a Python function that will do the following:1. Ask the user to input an integer...
Write a Python function that will do the following:1. Ask the user to input an integer which will be appended to a list that was originally empty. This will be done 5 times, meaning that when the input is complete, the list will have five elements (all integers).2. Determine whether each element is an even number or an odd number3. Return a list of five-string elements "odd" and "even" that map the indexes of the elements of the input list,...
In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
. Create a Python function that asks the user for a number (integer). The function should...
. Create a Python function that asks the user for a number (integer). The function should then tell the user how many hundreds can go into the number, and how much is left over. Hint: the % operator calculates the remainder of a division. For example, 10 % 3 gives a result 1. Hint2: Deal with the positive and negative values in separate parts of an if-else structure. Get the calculation work for positive values first. For negative values, make...
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
Flowchart + Python. Ask the user for a value. Then, ask the user for the number...
Flowchart + Python. Ask the user for a value. Then, ask the user for the number of expressions of that value. Use While Loops to make a program. So then, it should be so that 5 expressions for the value 9 would be: 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 Flowcharts and Python Code. Not just Python code. I cannot read handwriting....
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
c++ Write a program that will ask the user for three pairs of integer values. The...
c++ Write a program that will ask the user for three pairs of integer values. The program will then display whether the first number of the pair is multiple of the second. The actual work of making the determination will be performed by a function called IsMultiple that takes two integer arguments (say, x and y). The function will return a Boolean result of whether x is a multiple of y.
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
Write a Python program that will ask the user to input a word, will return the...
Write a Python program that will ask the user to input a word, will return the first letter of the input word, and ask the user to put another word, and so on, in the form of a loop. If the user chooses to stop, he or she should input the integer "0" for the loop to stop.
Write a mips assembly code program that ask the user to enter an integer value, and...
Write a mips assembly code program that ask the user to enter an integer value, and then print the result of doubling that number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT