110k views
5 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.

# TODO: Import the random module
import random
def number_guess(num):
# TODO: Get a random number between 1-100
# TODO: Read numbers and compare to random number

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)

1 Answer

3 votes

Answer:

Following are the code to this question:

import random #import package

def number_guess(num): #define method number_guess

n = random.randint(1, 100) #define variable n that hold random number

if num < n: #define if block to check gess number is less then Random number

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

elif num > n: #check number greater then Random number

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

else: #else block

print(num, "is correct!") #print message

if __name__ == '__main__': #define main method

random.seed(900) #use speed method

user_input = input() #define variable for user_input

tokens = user_input.split() #define variable that holds split value

for token in tokens: #define loop to convert value into string token

num = int(token) # convert token value in to integer and store in num

number_guess(num) # call number_guess method

Output:

33

33 is too low. Random number was 80.

Step-by-step explanation:

In the above-given python code, first, we import the random package, in the next line, a method number_guess is defined, that accepts num parameter, in this method, we store the random number in n variable and check the value from the conditional statement.

  • In this, we check value is less than from random number, it will print message too low, and its Random number. otherwise, it will go in the elif section.
  • In this section it will check the value is greater than from a random number, it will print the message to high, and its Random number. otherwise, it will go to else section.
  • In this, it will prints number and it is correct.
  • In the main method, we input value, and use a token variable to convert the value into string token, and then convert its value into an integer, and then call the method number_guess.
User Pseyfert
by
5.3k points