I have doubt abouty my random module code

Question:

Current behavior:

Desired behavior

Repl link:

code snippet

Judging from your repl profile, I’m assuming that this is about your repl, “day-4-start”?

Forgive me if I’m misunderstanding, but I think you are trying to generate a random number with random_float = random.random(), and then you are trying to multiply that number that it generates by 5?

The reason it’s not working is because in line 6, when you use random_float_5 = random.random() * 5 you are generating a new random number to multiply by 5 and not using the same number, (random_float,) as before from line 4.

Instead of generating a new random number, you should use the existing number.

For line 6 , random_float_5 = random.random() * 5 change this to
random_float_5 = random_float * 5

This is the same as saying give me a random number, and it gives you 2, Okay now multiply that number by 5.

Whereas what you had is saying, give me a random number and it gives you 2, and then you’re saying okay give me another random number and multiply that by 5.

This is because everytime you call random.random() it generates a new number, so if you want to use the same number that it already generated, you use it’s name instead of calling random.random() again.

a = random.random() # and it gives you 5
a_times_five = a * 5
print(a_times_five)

will output 25

Whereas

a = random.random() # and it gives you 5
different_random_number_x_five = random.random() * 5
print(different_random_number_x_five)

Will output some other number other than 25 because we’re generating a new different number , random.random(), instead of using a (which is 5.)

Does this help or did you mean something else?

3 Likes