scipiguy

Programming 102: Finding in lists + Counting

Jun 25th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. import random
  2.  
  3. def count(target, the_list):
  4.     counter = 0
  5.     for item in the_list:
  6.         if item == target_item:
  7.             counter += 1
  8.     return counter
  9.  
  10. ##################################################
  11. scores = []
  12.  
  13. for x in range (0, 30):
  14.   scores.append(random.randint(0, 10))
  15.  
  16. print(scores)
  17.  
  18. target_item = 10
  19. top_scorers = count(target_item, scores) # Count function called here
  20.  
  21. print("{0} learners got top marks".format(top_scorers))
  22.  
  23. ##################################################
  24.  
  25. pencils = ["red", "orange", "red", "green"]
  26.  
  27. target_item = input("What colour pencil are you looking for? ")
  28.  
  29. found_pencils = count(target_item, pencils)
  30. print("{0} of your pencils were found".format(found_pencils))
  31.  
  32. ##################################################
Advertisement
Add Comment
Please, Sign In to add comment