""" Felix Muzny DS 2000 Lecture 10 - Oct. 11 Putting the *fun* into functions! """ import random # write a function that: # 1. rolls two dice # 2. returns their sum def roll_two_dice(): """ Rolls two dice and returns the sum. No parameters :return: int sum of two dice """ die1 = random.randint(1, 6) die2 = random.randint(1, 6) total = die1 + die2 return total # Think about: # a function that makes a list of some given length (int parameter) # of roll_two_dice values # Functions and returns # functions will EITHER display (printing, plotting) OR # do a computation (return of the result, no printing, no plotting) # write a function that: # 1. takes an int as a parameter # 2. print all numbers from 0 to that number [inclusive] (one # number per line is fine) def print_ints(max_num): """ Prints numbers from 0 to the int given, including that one. :param max_num: int number to go up to :return: nothing """ for i in range(max_num + 1): print(i) # write a function that: # 1. takes a list as a parameter # 2. prints each sublist in it, always starting from index 0 def print_sublists(ls): """ Prints every sublist from the empty list to the entire list given, always starting with the first element :param ls: list to print the sublists of :return: nothing """ # trust that ls has a value that is a list # print(ls[:0]) # [] # print(ls[:1]) # [first] # print(ls[:2]) # [first, second] for i in range(len(ls) + 1): print(ls[:i]) def main(): print("lecture 10 - FUNctions!") print("Test roll_two_dice:") sum1 = roll_two_dice() print(sum1) print(roll_two_dice()) print() # calling the new print_ints function print("Test print_ints") print_ints(4) print() print_ints(10) print() print("test print_sublists") print_sublists([2, 6, 7, 10]) main()