Changes between Version 16 and Version 17 of ControlSystems/SoftwareTeam/IntroToPython


Ignore:
Timestamp:
Sep 4, 2017, 5:03:41 PM (8 years ago)
Author:
cdelgigante
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/IntroToPython

    v16 v17  
    4141
    4242After a bit, you'll  be rewarded with the message in the bottom window:
     43
    4344{{{
    4445Hello World!
     
    5758if location == 'HERE':
    5859   print("We're here! Come on in\n")
    59    print("Our ours are 11:00am-11:52am")
     60   print("Our ours are 11:00am-11:52am\n")
    6061else:
    6162   print("Go away, we're out eating tacos\n")
    62 print("Hope you had a good time")
     63print("Hope you had a good time\n")
    6364}}}
    6465It should print:
     66
    6567{{{
    66 Welcome to our store                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          We're here! Come on in                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        Hope you had a good time 
     68Welcome to our store                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          We're here! Come on in                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        Our ours are 11:00am-11:52am                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  Hope you had a good time                                                                                                                                                                                                                                                    
    6769}}}
     70Three things to note:
    6871
    69 Three things to note:
    70 1. The double equals in location=='HERE' on the first line.   It is called the ''condition''. This is a equals comparison.  Other kinds of comparisons in python are listed here.   Also note on that line 'HERE' is in quotes.  That's to indicate its a string (a text value) not some variable. 
    71 2.  Spacing and indents in Python are important.  the line "We're here..." and the line below it are indented to indicate its a part of a statement ''block'' to run when the condition is ''true''.   The second block on the else is run when the condition is ''false'', and only contains one lines.   If you want to do stuff outside of the IF-THEN, don't indent it like the first and last print statements.
     72 1. The double equals in location=='HERE' on the first line.   It is called a ''condition''. This is a equals comparison.  Other kinds of comparisons in python are listed here.   Also note on that line 'HERE' is in quotes.  That's to indicate its a string (a text value) not some variable.
     73 1. Spacing and indents in Python are important.  the line "We're here..." and the line below it are indented to indicate its a part of a statement ''block'' to run when the condition evaluates to ''True''.   The second block on the else is run when the condition to ''False'', and only contains one line.   If you want to do stuff outside of the IF-THEN, don't indent it like the first and last print statements.
     74 1. You'll see that a few of the print statements end with a {{\\n}}.  This is called a ''escape sequence'', and its shorthand for a newline.  When the computer sees it in a print statement, it will add an extra line to the output. See here for other escape sequences you can add.
    7275
    73 Now, change line one to Location='Poughkeepsie, NY'.  It should print "Go away, we're out eating tacos".   If I was in Poughkeepsie, I'd be out eating tacos too.  Yummmm, tacos.
     76Now, change the ''condition'' to Location=='Poughkeepsie, NY'.  It should print "Go away, we're out eating tacos".   What happens?   Why?  If I was in Poughkeepsie, I'd be out eating tacos too.  Yummmm, tacos.
    7477
    7578This control struction evaluates a *expression* (location=='HERE') and if its true, then do the THEN part, otherwise do the ELSE part.   This is called the IF-THEN-ELSE structure, and it’s the most basic control structure.