Advertisement
timber101

Counting Functions

Dec 24th, 2020
974
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | None | 0 0
  1. from random import randint
  2.  
  3. #  function to count a specific grade
  4. def my_count(tgt, list):
  5.     cnt = 0
  6.     for sc in list:
  7.         if sc == tgt:
  8.             cnt +=1
  9.     print(f"{cnt} learners with a score of {tgt}")
  10.    
  11. #  function to count all grades, so just needs on parameter
  12. def my_count_all(list):
  13.     for tgt in range(10,-1,-1): # to start at 10 and stop at 0
  14.         cnt = 0
  15.         for sc in list:
  16.             if sc == tgt:
  17.                 cnt +=1
  18.         print(f"{cnt} learners with a score of {tgt}")
  19.     print("__")
  20.     print(f"{len(scores)} learners altogether")
  21.     print("==")
  22.    
  23. #  function to count a grade range
  24. def my_count_range(minz, maxz, list):
  25.     cnt = 0
  26.     for sc in list:
  27.         if sc in range(minz,maxz):
  28.             cnt +=1
  29.     print(f"{cnt} learners with a score above {minz} and below {maxz}")
  30.    
  31. #  function to count vowles in a word
  32. def count_vowels(word):
  33.     cnt = 0
  34.     for i in range(0,len(word)):
  35.         if word[i].lower() in my_vowels:
  36.             cnt +=1
  37.     print(f"there are {cnt} vowels in the word {word}")
  38.  
  39.  
  40. my_list_of_towns = ["Blackfield","Whitefield","Greyfield","Nofield", "Bigfield", "Blackfield"]
  41. scores = []
  42. my_vowels = ["a", "e", "i", "o", "u"]
  43.  
  44. for x in range (0, 30):
  45.   scores.append(randint(0, 10))    # Generate a random number from 0 to 10 and append to scores
  46.  
  47. #print(scores)
  48. '''
  49. for score in scores:
  50.    if score == 10:
  51.        tens +=1
  52. '''
  53.  
  54.  
  55. #print(scores.count(10)) testing
  56. #print(f"{tens} learners got top marks")
  57.  
  58.  
  59. # function call my_count
  60. #my_count(10,scores)
  61. #my_count(9,scores)
  62. #my_count(8,scores)
  63. #my_count(7,scores)
  64.  
  65. # function call my_count_all
  66. my_count_all(scores)
  67.  
  68. # function call for my_count_range
  69. my_count_range(0,11,scores)  ## not to self, starts at first value, stops before the second
  70.  
  71. # call using my_list_of_towns list - works but should have narative changed
  72. my_count("Blackfield",my_list_of_towns)
  73.  
  74. ##my_count("grapes", my_vowels)  didnt work! going to create a new function
  75.  
  76. # calling count_vowels
  77. count_vowels("Argh")
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement