I have some questions on the random module. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I have some questions on the random module.

I am working on a game that requires random, and I need to mention it two times with the same result: greetings = ['hello', 'hi'] x = random.randrange[greetings(2)] print(x) print(x) however the results of 'x' are different between the two prints. May I ask how to make them the same?

18th Nov 2017, 9:51 AM
Cat🐈
Cat🐈 - avatar
3 Answers
+ 2
Greetings is a list, and not callable. If you're just trying to get a random selection from a list, try choice instead: x = random.choice(greetings)
18th Nov 2017, 10:43 AM
Sapphire
+ 2
yes, but may I ask how do you print 'x' two times with for example hello?
18th Nov 2017, 10:45 AM
Cat🐈
Cat🐈 - avatar
+ 1
That shouldn't be possible for it to print two different results from the same variable. When you call the random library, and use the choice function, it returns the result and saves it into x. you can call it a hundred times and x will not change until you assign it another value. If this is happening in your game, it's possible you're either calling random twice or x is being assigned more than once.
18th Nov 2017, 10:48 AM
Sapphire