Guest User

Untitled

a guest
May 21st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. import random
  2.  
  3. words = ["Please", "Help", "Me", "Merry", "Christmas"]
  4.  
  5. for i in range(len(words)):
  6. random_index = random.randrange(len(words))
  7. print(words[random_index])
  8. del words[random_index]
  9.  
  10. >>> import random
  11. >>> words = ["Please", "Help", "Me", "Merry", "Christmas"]
  12. >>> random.sample(words, len(words))
  13. ['Merry', 'Me', 'Help', 'Please', 'Christmas']
  14.  
  15. >>> random.shuffle(words)
  16. >>> words
  17. ['Me', 'Merry', 'Help', 'Please', 'Christmas']
  18.  
  19. import random
  20.  
  21. myList = ["create", "a", "program", "that", "prints"]
  22.  
  23. for i in range(len(myList)):
  24. randomWord = random.choice(myList)
  25. print(randomWord)
  26. myList.remove(randomWord)
  27.  
  28. input("nPress enter to exit.")
  29.  
  30. import random
  31. random.shuffle(words)
  32. print words
  33.  
  34. import random
  35.  
  36. words = ["Please", "Help", "Me", "Merry", "Christmas"]
  37.  
  38. # in-place or copy shuffle
  39. def shuffle(in_words, copy=False):
  40. in_words = in_words[:] if copy else in_words
  41. for i in range(len(in_words)):
  42. pos = random.randrange(len(in_words))
  43. in_words[i], in_words[pos] = in_words[pos], in_words[i]
  44. return in_words
  45. # see if it works
  46. print "unshuffled", words
  47. print "shuffled %s (was: %s)" % (shuffle(words, copy=True), words)
  48. print "in-place shuffled", shuffle(words)
  49.  
  50. words = ["Please", "Help", "Me", "Merry", "Christmas"]
  51.  
  52. for i in range(len(words)):
  53. random_index = random.randrange(len(words))
  54. print(words.pop(random_index))
  55.  
  56. import random
  57.  
  58. words = ["Please", "Help", "Me", "Merry", "Christmas"]
  59. wordsUsed = []
  60. print()
  61.  
  62. while len(wordsUsed) != len(words):
  63. choice = random.choice(words)
  64. if choice not in wordsUsed:
  65. print(choice)
  66. wordsUsed.append(choice)
  67.  
  68. input("nPress the enter key to exit..")
  69.  
  70. list=["hello","bye","good night","welcome"]
  71.  
  72. new_list=[]
  73.  
  74. import random
  75.  
  76.  
  77. while len(new_list)!=4:
  78.  
  79. word=random.choice(list)
  80.  
  81. if word not in new_list:
  82.  
  83. new_list.append(word)
  84.  
  85. else:
  86.  
  87. new_word.remove(word)
  88.  
  89. print(new_list)
Add Comment
Please, Sign In to add comment