#!/usr/bin/env python # coding: utf-8 # In[1]: # Section 5.5 continued # Keyword arguments # You've done this before: print("abc","def","ghi",sep='XYZ') # here -- the code # # sep='XYZ' is specifying # # a keyword parameter # Example program with simple interest # FYI: simple interest=int_rate*principal*periods def main(): show_interest(rate=0.01,periods=10,principal=10000) #show_interest(0.01,10,10000) def show_interest(principal, rate, periods): interest=rate*principal*periods print("The rate is",rate) print("The simple interest will be $",format(interest,'.2f'),sep='') main() # In[4]: # Section 5.6 -- global variables and global constants # Definition: A global variable can be accessed by any function. # Example my_value=10 def show_value(): print(my_value) show_value() # In the previous example -- a global variable was defined # outside of the function and then used inside of the # function. # # Now we will define a global variable from within a function. # Generally: this is bad behavior -- avoid as much as possible # def main(): global number # avoid this when you can -- usually you can number=int(input("Give me an integer.")) show_number() def show_number(): print("Your number is",number) main() print(number) # this is surprising, because the number # # variable was NOT destroyed when the main() # # function terminated -- this is what the keyword # # global made happen # # why avoid this: makes debugging a nightmare # In[8]: # Section 5.7 - value-returning functions # idea: A value-returning function "returns" a value when it terminates # so far -- all of our functions terminate without "returning" a value # this is different than "printing" something # # Importing packages -- we've done this before: turtle, time # Look at the random package # # # Import the random module: import random # # Generally: a module is thought of as a "black box" meaning # you feed functions inputs and they give outputs, but you # don't really think too much about "how" they do it. # # # # look at some functions in the random package # Display a "random" integer between 1 and 10 def main(): number=random.randint(1,10) print("Your random number is",number) main() main() main() main() main() main() main() main() main() main() main() main() main() main() main() main() # In[10]: # Die roller program # Generate two random numbers between 1 and 6 # User should be asked if they want to roll again import random MIN=1 MAX=6 def main(): # loop control -- use a control variable go_again='y' while go_again=='y': print("Rolling...") print("First die roll is",random.randint(MIN,MAX)) print("Second die roll is",random.randint(MIN,MAX)) go_again=input("Write y to roll again, n to stop.") main() # In[27]: # Some more funcitons in random module import random #random.randrange(#) # this picks a random number between 0 and #-1 random.randrange(7) #random.randrange(A,B) # pick random between A and B-1 #random.randrange(A,B,C) # pick random among A, A+C, A+2C, ..., A+kC where A+kC < B < A+(k-1)C # #Example: generate a random even number between 0 and 10 #random.seed(10) # all randomness depends on the seed print(random.randrange(0,11,2)) print(random.randrange(0,11,2)) print(random.randrange(0,11,2)) print(random.randrange(0,11,2)) print(random.randrange(0,11,2)) print(random.randrange(0,11,2)) print(random.randrange(0,11,2)) print(random.randrange(0,11,2)) # random function print("this gives a number x such that 0<=x<1",random.random()) # uniform function print("this gives a number x such that A<=x