timber101

Functions Example

Jan 1st, 2022 (edited)
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | None | 0 0
  1. #  Functions example
  2. #  by Colin
  3.  
  4. #  Houses - Sopley, Ibsley, Hurn, Holmsley, Beaulieu
  5.  
  6. """
  7. The function count_points() counts how many points have been earned by pupils in College.  The name of the house is passed as a string parameter and the number of points earned is returned as an integer
  8.  
  9. Write code using the function count_points() to find the number of points awarded to pupils in Hurn and assign this value to a variable with identifier hurn_points
  10.  
  11. """
  12.  
  13. """
  14.  
  15. def function_name(parameter):
  16.    #do stuff with the parameter
  17.    return
  18.  
  19. def double(anumber):
  20.    return anumber*2
  21.  
  22. print(double(5))
  23.  
  24. """
  25. #  Functions example
  26. #  by Colin
  27.  
  28. #  Houses - Sopley, Ibsley, Hurn, Holmsley, Beaulieu
  29.  
  30. """
  31. # Here we go!
  32.  
  33. import random
  34.  
  35. houses =  ["Sopley", "Ibsley", "Hurn", "Holmsley", "Beaulieu"]
  36.  
  37. points_awarded =[] # empty list
  38.  
  39. for i in range(100):
  40.    points_awarded.append(random.choice(houses))
  41.    
  42.  
  43. def count_points(house):
  44.    points_count = points_awarded.count(house)
  45.    return points_count
  46.  
  47. hurn_points = count_points("Sopley")
  48.  
  49. print (hurn_points)
  50.    
  51. """
  52. Exam only needs this line
  53.  
  54. hurn_points = count_points("Hurn")
  55.  
  56. """
  57.  
  58. """
  59. Looking for a bit more fun?
  60.  
  61. Can you change the function to print out a count of all the houses and points?
  62. Output to look like this
  63.  
  64. Hurn     20
  65. Sopley   20
  66. Ibsley   20
  67. Holmsley 20
  68. Beaulieu 20
  69.  
  70. Total   100
  71.  
  72. """
  73. Feedback please to Mr Benson or me!
  74. """
  75. #  my first stab at the extension
  76. def count_points(house):
  77.     if house == "All":
  78.         print ("Hurn      ", points_awarded.count("Hurn"))
  79.         print ("Ibsley    ", points_awarded.count("Ibsley"))
  80.         print ("Beaulieu  ", points_awarded.count("Beaulieu"))
  81.         print ("Sopley    ", points_awarded.count("Sopley"))
  82.         print ("Holmsley  ", points_awarded.count("Holmsley"))
  83.         print ("Total    ",len(points_awarded))
  84.     else:
  85.         points_count = points_awarded.count(house)
  86.         return points_count
  87.  
  88. hurn_points = count_points("All")
  89.  
  90. Please talk to me if you have a go at this, I think there are some things wrong, but the gist is there!
  91. Colin    
Add Comment
Please, Sign In to add comment