Ask Question

Need 2.5 Code Practice Answers

Copy and paste the following code into the sandbox window. Modify the code so that user is asked to multiply two random numbers from 1 to 10 inclusive.

import random

random. seed ()

# TODO: Update the following two lines with a call to a function

# from the random library that generates a random number between

# 1 and 10, inclusive.

a = 6 # HINT: Replace 6 with a function call

b = 3 # HINT: Replace 3 with a function call

print ("What is: " + str (a) + " X " + str (b) + "?")

ans = int (input ("Your answer: "))

if (a * b = = ans):

print ("Correct!")

else:

print ("Incorrect!")

+2
Answers (2)
  1. 7 February, 21:02
    0
    import random

    random. seed (1,10)

    a = random. randint (1,10)

    b = random. randint (1,10)

    print ("What is: " + str (a) + " X " + str (b) + "?")

    ans = int (input ("Your answer: "))

    if (a * b = = ans):

    print ("Correct!")

    else:

    print ("Incorrect!")
  2. 7 February, 21:19
    0
    import random

    #random. seed () should not be used here as this method will produce same number again and again

    a = random. randint (1,11) #this method will generate random number between 1 and 10

    b = random. randint (1,11)

    print ("What is: " + str (a) + " X " + str (b) + "?")

    ans = int (input ("Your answer: "))

    if (a * b = = ans):

    print ("Correct!")

    else:

    print ("Incorrect!")

    Output:

    What is: 8 X 5?

    Your answer: 40

    Correct!

    Explanation:

    First for producing random numbers, random module has to be imported. As mentioned in answer, random. seed () method is used to generate same random number again and again. Here we need to generate 2 different random numbers that is why we won't use this method. To produce a random number, randint () method from random module is used. This method takes 2 parameters i. e. a low and high value. The low value is inclusive and the high value is exclusive. That is why, to get a number between [1,10], randint () takes 1 as low value (inclusive) and 11 as high value (exclusive).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Need 2.5 Code Practice Answers Copy and paste the following code into the sandbox window. Modify the code so that user is asked to multiply ...” 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