Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- words = ["Please", "Help", "Me", "Merry", "Christmas"]
- for i in range(len(words)):
- random_index = random.randrange(len(words))
- print(words[random_index])
- del words[random_index]
- >>> import random
- >>> words = ["Please", "Help", "Me", "Merry", "Christmas"]
- >>> random.sample(words, len(words))
- ['Merry', 'Me', 'Help', 'Please', 'Christmas']
- >>> random.shuffle(words)
- >>> words
- ['Me', 'Merry', 'Help', 'Please', 'Christmas']
- import random
- myList = ["create", "a", "program", "that", "prints"]
- for i in range(len(myList)):
- randomWord = random.choice(myList)
- print(randomWord)
- myList.remove(randomWord)
- input("nPress enter to exit.")
- import random
- random.shuffle(words)
- print words
- import random
- words = ["Please", "Help", "Me", "Merry", "Christmas"]
- # in-place or copy shuffle
- def shuffle(in_words, copy=False):
- in_words = in_words[:] if copy else in_words
- for i in range(len(in_words)):
- pos = random.randrange(len(in_words))
- in_words[i], in_words[pos] = in_words[pos], in_words[i]
- return in_words
- # see if it works
- print "unshuffled", words
- print "shuffled %s (was: %s)" % (shuffle(words, copy=True), words)
- print "in-place shuffled", shuffle(words)
- words = ["Please", "Help", "Me", "Merry", "Christmas"]
- for i in range(len(words)):
- random_index = random.randrange(len(words))
- print(words.pop(random_index))
- import random
- words = ["Please", "Help", "Me", "Merry", "Christmas"]
- wordsUsed = []
- print()
- while len(wordsUsed) != len(words):
- choice = random.choice(words)
- if choice not in wordsUsed:
- print(choice)
- wordsUsed.append(choice)
- input("nPress the enter key to exit..")
- list=["hello","bye","good night","welcome"]
- new_list=[]
- import random
- while len(new_list)!=4:
- word=random.choice(list)
- if word not in new_list:
- new_list.append(word)
- else:
- new_word.remove(word)
- print(new_list)
Add Comment
Please, Sign In to add comment