Advertisement
HJ50

quiz_count

Apr 5th, 2020
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. '''
  2. quiz scores
  3. '''
  4.  
  5. from random import randint
  6.  
  7. #count items in a list
  8. def count_items(target, my_list):
  9.    
  10.     item_count=0
  11.     for i in my_list:
  12.         if i==target:
  13.             item_count+=1
  14.    
  15.     return item_count
  16.  
  17. #count items in a range (passed as [lower, upper])
  18. def count_in_range(target_range, my_list):
  19.    
  20.     my_count=0
  21.     for i in my_list:
  22.         if i>=target_range[0] and i<=target_range[1] :
  23.             my_count+=1
  24.    
  25.     return my_count
  26.    
  27. #count vowels in a string
  28. def count_vowels_in_string(my_string):
  29.     vowels="aeiou"
  30.     my_count=0
  31.     lc_string=my_string.lower()
  32.     for i in vowels:
  33.         my_count=my_count+lc_string.count(i)
  34.            
  35.     return my_count
  36.    
  37. #data to work with
  38. scores=[]
  39. for i in range(30):
  40.     scores.append(randint(1,10))
  41. names=["fred", "bert", "harry", "fred"]
  42. my_word="supercalifragilisticexpialidociousUMDIDDLEIDDLEIDDLE"
  43.  
  44. #print top scorers and scorers in a range 7-9
  45. print("Scores", scores)
  46. print("{0} learners got top marks".format(count_items(10,scores)))
  47. print("{0} learners got between 7 and 9 (inc)".format(count_in_range([7,9],scores)))
  48.  
  49. #works for lists of strings too
  50. print("Names", names)
  51. print("{0} freds".format(count_items("fred",names)))
  52.  
  53. #count vowels in a string
  54. print("Word", my_word)
  55. print("{0} vowels in word".format(count_vowels_in_string(my_word)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement