Advertisement
Antypas

Count numbers or strings

Apr 14th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. # Counting occurrence in a lsit, whether numbers or strings
  2.  
  3. import random
  4.  
  5. # Add your count function here
  6. def count(target_grade,scores_list):
  7.     target_hits = 0
  8.     for score in scores_list:
  9.         if score == target_grade:
  10.             target_hits += 1
  11.     return target_hits, target_grade
  12.  
  13.  
  14. # "Create" a list of scores, random integeres, then convert
  15. # it to a list of strings
  16. scores = []
  17. stringed_scores = []
  18.  
  19. for x in range (0, 30):
  20.     scores.append(random.randint(0, 10))
  21.     stringed_scores.append(str(scores[x]))
  22.  
  23. print("list of scores ", scores)
  24. print("list of  stringed scores ", stringed_scores)
  25.  
  26. print(count('5', stringed_scores))
  27.  
  28. print("try another list of strings")
  29. mestring= ["me", "ego", "selfish", "pig", "pig", "me", "mine", "pig"]
  30. print(mestring)
  31. print(count("pig", mestring))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement