77.8k views
2 votes
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

2 Answers

6 votes

Answer:

Correct answer in Python

Please don't forget to upvote me if it's correct

It's correct especially those looking for the answer in Zybooks

Step-by-step explanation:

import random

def number_guess(num):

n = random.randint(1, 100)

if num < n:

print(num, "is too low. Random number was " + str(n) + ".")

elif num > n:

print(num, "is too high. Random number was " + str(n) + ".")

else:

print(num, "is correct!")

if __name__ == '__main__':

# Use the seed 900 to get the same pseudo random numbers every time

random.seed(900)

# Convert the string tokens into integers

user_input = input()

tokens = user_input.split()

for token in tokens:

num = int(token)

number_guess(num)

User HamzaMushtaq
by
5.4k points
4 votes

Answer:

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

Step-by-step 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.
User Dvdgsng
by
5.4k points