Changes between Version 60 and Version 61 of ControlSystems/SoftwareTeam/IntroToPython


Ignore:
Timestamp:
Sep 4, 2017, 8:02:16 PM (8 years ago)
Author:
cdelgigante
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/IntroToPython

    v60 v61  
    253253}}}
    254254
    255 But wait, there's more...you can create your own functions.  That way if you come up with cool way to do something you dont have to repeat yourself as much.  For instance:
    256 
     255'''But wait, there's more'''...you can create your own functions.  That way, if you come up with cool way to do something you can reuse it and don't have to repeat yourself as much.  For instance:
     256
     257{{{
     258def singHappyBirthday(age, names): # a custom function
     259   for name in names:
     260      for count in range(0, age):
     261         print("Happy Birthday to You!")
     262         if count == age-2:
     263           print("Happy Birthday, Dear", name)
     264       
     265singHappyBirthday(6,['Milla', 'Emma'])  # now use it.
     266}}}
     267
     268Run this one.  What does it output?  A lovely happy birthday song for two kids?   Imagine if you had to write a program to sing happy birthday to all the people in your class without it.  It'd get pretty ugly, fast.
     269
     270`def` is used to indicate a custom function, and you have to define (that's what def is short for) it in your program before you use it.  It takes an argument of an age, and a list of names.  The "singHappyBirthday" line actually ''calls'' the custom function, with arguments of an age of size and a list of two names.
     271
     272
     273Now what happens if you try and run it with age equal to 1?  Modify the program it prints out "Aw a baby!' if an age of 1 is used.
    257274
    258275