Advertisement
jspill

webinar-functions-2021-10-09--v4.py

Oct 9th, 2021
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. # Webinar: Functions + Methods in Python...
  2.  
  3. # You'll start getting to know functions in Ch 7
  4.  
  5. # functions should be modular and reusable
  6. # we define a function once and call it as many times as needed
  7.  
  8. def myFunction(parameter):
  9.     # don't do this:
  10.     # parameter = 5 # no!
  11.     print(someValue) # or
  12.     return someValue
  13.  
  14. # return and print() are different
  15.  
  16. # function definition, Gallant
  17. def squareThis(num):
  18.     # square = num * num
  19.     # return square
  20.     return num * num
  21.  
  22. # squareThis(3)
  23. print(squareThis(3)) # 9
  24. print(squareThis(5)) # 25
  25. x = squareThis(8)
  26. print(x) # 64
  27.  
  28. # function definition, Goofus
  29. def squareSomething(num):
  30.     num = 5 # no, Goofus! Don't override your parameter!
  31.     return num * num
  32.  
  33. print("Goofus:")
  34. print(squareSomething(3)) # Goofus hopes you like 25
  35. print(squareSomething(6)) # on every...
  36. print(squareSomething(9)) # single call :(
  37.  
  38. # help(str)
  39. print(dir(list)) # returns a list
  40. help(list.append) # append() is a list METHOD, a function particular to that type
  41.  
  42. for item in dir(list): # dir() returns a list, so I can treat a call to dir() as a list
  43.     if not item.startswith("__"):
  44.         print(item)
  45.  
  46. # Ch 8 Task 1
  47. # Complete the function to print the first X
  48. # number of characters in the given string
  49. def printFirst(mystring, x):
  50.     print(mystring[0:x])
  51. # expected output: WGU
  52. printFirst('WGU College of IT', 3)
  53. # expected output: WGU College
  54. printFirst('WGU College of IT', 11)
  55. # expected output: WKRP
  56. printFirst("WKRP in Cincinnati", 4)
  57.  
  58. # The new LABS are your best preparation for the new Pre and OA!
  59. # We looked at Lab 24.3...
  60.  
  61. # Define your function here -- ABOVE THE IF STATEMENT that checks the script name
  62. def seconds_to_jiffies(user_seconds):  # probably safe to change parameter name, but not function name!
  63.     return user_seconds * 100
  64.  
  65. if __name__ == '__main__':
  66.     # Type your code here. Your code must call the function.
  67.     # help(list) # help() and dir() work on Labs and test system!
  68.     # (but don't leave those calls in your finished code)
  69.     user_input = float(input())
  70.     num_jiffies = seconds_to_jiffies(user_input)  # user_input isn't the PARAMETER, it's an ARGUMENT
  71.     # seconds_to_jiffies(5) # here 5 would be the argument
  72.     print('{:.2f}'.format(num_jiffies))
  73.    
  74.     # btw, I would probably have just written all of that like this:
  75.    
  76.     # print('{:.2f}'.format(seconds_to_jiffies(float(input()))))
  77.    
  78.     # ... but no harm at all in breaking it out into more lines!
  79.     # If it helps to see it step by step, then do it step by step!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement