Ask Question

Given the code that reads a list of integers, complete the number_guess () function, which should choose a random number between 1 and 100 by calling random. randint () and then output if the guessed number is too low, too high, or correct.

Import the random module to use the random. seed () and random. randint () functions.

random. seed (seed_value) seeds the random number generator using the given seed_value.

random. randint (a, b) returns a random number between a and b (inclusive).

For testing purposes, use the seed value 900, which will cause the computer to choose the same random number every time the program runs.

Ex: If the input is:

32 45 48 80

the output is:

32 is too low. Random number was 80.

45 is too high. Random number was 30.

48 is Correct.

80 is too low, Random number was 97

+5
Answers (1)
  1. 6 May, 17:11
    0
    The following are the program in the Python Programming Language

    #import package

    import random

    random. seed (900)

    #define a function

    def number_guess ():

    #return random number from 1 to 100

    return random. randint (1,100)

    #get input in the variable

    st = input ('Enter numbers: ')

    #declare list type variable and split its elements

    lst = [int (i) for i in st. split () ]

    #set the for loop

    for i in range (10):

    #declare variable that stores the output of the function

    n = number_guess ()

    #check the value of n in the variable lst

    if n in lst:

    #then, print correct if the number is matched

    print (n, 'is correct!')

    #print space

    print ('')

    #otherwise,

    else:

    #set the for loop

    for k in lst:

    #set the if conditional statement

    if abs (k-n) >=15:

    #check that k is greater that n

    if k>n:

    #then, print that if the number is higher

    print (k,'is too high')

    #otherwise, print that if the number is smaller

    else:

    print (k,'is too low')

    #print the random number

    print ('Random number was', n)

    #print space

    print ('')

    #break the loop

    break

    Output:

    80 is correct!

    45 is too high

    Random number was 30

    48 is correct!

    32 is too low

    Random number was 97

    32 is too high

    Random number was 13

    32 is too low

    Random number was 85

    32 is too high

    Random number was 11

    32 is too low

    Random number was 90

    32 is correct!

    32 is too low

    Random number was 93

    Explanation:

    The following are the description of the program.

    Firstly, import the package that is 'random' then, using the seed value 900. Then, define a function number_guess () that returns and generate the numbers from 1 to 100. Declare variable 'st' that get input from the user and set list type variable 'lst' that splits the following input by the variable 'st'. Set the for loop that iterates 10 times then, set the variable 'n' that stores the return values by the function. Then, set the for loop that checks the value of the variable 'n' in the variable 'lst' then, print the following number with message.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Given the code that reads a list of integers, complete the number_guess () function, which should choose a random number between 1 and 100 ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers