""" Purpose: introduce some another function from module random """ # by default the random module produces different interactions everytime we use it import random # always import the library random --> will give you access to functions inside it #random.seed( 1112 ) # random.seed( something ) # use as the "starting point" of the random machine # can be ANYTHING # will give you the SAME "random" results every time you rerun the program # random function choice( seq ) when a given a sequence as a parameter returns # a random element from the sequence ### get a string and choose four of its characters (repetition allowed) s = input( "Enter a string: " ) # random.choice( sequence ) # sequence can be a string --> get a random character from string # different every time you run the program # different for every machine i1 = random.choice( s ) i2 = random.choice( s ) i3 = random.choice( s ) i4 = random.choice( s ) print () print( "Four random characters from string", s, ": ", i1, i2, i3, i4 ) print () ### get a list of words and choose four (repetition allowed) reply = input( "Enter some words: " ) user_words = reply.split() # get list of words from the reply # here the sequence is a list, so random.choice() will choose a random ELEMENT in the list instead w1 = random.choice( user_words ) w2 = random.choice( user_words ) w3 = random.choice( user_words ) w4 = random.choice( user_words ) print( "Four random words from list", user_words, ":", w1, w2, w3, w4 )