Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Webinar: Functions + Methods in Python...
- # You'll start getting to know functions in Ch 7
- # functions should be modular and reusable
- # we define a function once and call it as many times as needed
- def myFunction(parameter):
- # don't do this:
- # parameter = 5 # no!
- print(someValue) # or
- return someValue
- # return and print() are different
- # function definition, Gallant
- def squareThis(num):
- # square = num * num
- # return square
- return num * num
- # squareThis(3)
- print(squareThis(3)) # 9
- print(squareThis(5)) # 25
- x = squareThis(8)
- print(x) # 64
- # function definition, Goofus
- def squareSomething(num):
- num = 5 # no, Goofus! Don't override your parameter!
- return num * num
- print("Goofus:")
- print(squareSomething(3)) # Goofus hopes you like 25
- print(squareSomething(6)) # on every...
- print(squareSomething(9)) # single call :(
- # help(str)
- print(dir(list)) # returns a list
- help(list.append) # append() is a list METHOD, a function particular to that type
- for item in dir(list): # dir() returns a list, so I can treat a call to dir() as a list
- if not item.startswith("__"):
- print(item)
- # Ch 8 Task 1
- # Complete the function to print the first X
- # number of characters in the given string
- def printFirst(mystring, x):
- print(mystring[0:x])
- # expected output: WGU
- printFirst('WGU College of IT', 3)
- # expected output: WGU College
- printFirst('WGU College of IT', 11)
- # expected output: WKRP
- printFirst("WKRP in Cincinnati", 4)
- # The new LABS are your best preparation for the new Pre and OA!
- # We looked at Lab 24.3...
- # Define your function here -- ABOVE THE IF STATEMENT that checks the script name
- def seconds_to_jiffies(user_seconds): # probably safe to change parameter name, but not function name!
- return user_seconds * 100
- if __name__ == '__main__':
- # Type your code here. Your code must call the function.
- # help(list) # help() and dir() work on Labs and test system!
- # (but don't leave those calls in your finished code)
- user_input = float(input())
- num_jiffies = seconds_to_jiffies(user_input) # user_input isn't the PARAMETER, it's an ARGUMENT
- # seconds_to_jiffies(5) # here 5 would be the argument
- print('{:.2f}'.format(num_jiffies))
- # btw, I would probably have just written all of that like this:
- # print('{:.2f}'.format(seconds_to_jiffies(float(input()))))
- # ... but no harm at all in breaking it out into more lines!
- # 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