I started up with lesson 3 and went up lesson 4 Lesson 4 : Conditionals and Control Flow
Boolean Operators.
"""
Learning Python
Lesson 3 : Strings
Advanced Printing
Simple programming exercises from Codecademy
"""
# Exercise 0 : String Concatenation
"""
Create a variable called breakfast and set it to the
concatenated strings "Spam ", "and ", "eggs".
Then print breakfast to the interactions window.
"""
breakfast = "Spam " + "and " + "eggs"
print breakfast
# Exercise 1 : Explicit String Conversion
"""
Uncomment the print statement below, SAVE and RUN.
What happens?
Use str() to turn 3.14 into a string and SAVE and RUN again.
"""
print "The value of pi is around " + str(3.14)
#Exercise 2 : String Formatting with %, Part 1
"""
Uncomment the print statement below (Remove the # symbol).
What do you think it'll do?
Once you think you know, SAVE and RUN.
"""
string_1 = "Camelot"
string_2 = "place"
print "Let\'s not go to %s. \'Tis a silly %s." % (string_1, string_2)
# Exercise 3 : Print Formatting
"""
For our grand finale, we're showing you a bit of new code.
Don't worry if you don't get how it works yet; we'll explain it soon!
Uncomment the lines below and replace the ___s with the form of %
you need to complete your quest:
%s inside the string, and % to link the string with its arguments.
Answer the questions in the console as they pop up!
"""
name = raw_input("What is your name?")
quest = raw_input("What is your quest?")
color = raw_input("What is your favorite color?")
print "Ah, so your name is %s, your quest is %s, and your favorite color is %s." % (name, quest, color)
# ************************************************************
# TESTS : DO NOT MODIFY THESE
# ************************************************************
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
>>>
Spam and eggs
The value of pi is around 3.14
Let's not go to Camelot. 'Tis a silly place.
What is your name? Joseph M Konneh
What is your quest? Learning
What is your favorite color? Green
Ah, so your name is Joseph M Konneh, your quest is Learning , and your favorite color is Green.
>>>
Here are the results of Lesson 4: Conditionals
Exercise 0 : Test Failed!
Exercise 1 : Test Failed!
>>>
0
0
0
0
0
0
>>>
True
False
True
False
True
False
Here are the results of Lesson 4: Conditionals
Exercise 0 : Test Passed!
Exercise 1 : Test Passed!
"""
Learning Python
Lesson 4 : Conditionals and Control Flow
Comparators
Simple programming exercises from Codecademy
"""
# IGNORE THIS CODE
# *******************************************************************************************************************************************************
bool1 = bool2 = bool3 = bool4 = bool5 = bool6 = bool7 = bool8 = bool9 = bool10 = bool11 = 0
# *******************************************************************************************************************************************************
# Exercise 0 : Compare closely!
"""
Set each variable to either True or False depending on what you
think the result of the evaluation above it will be.
For example, 1 < 2 will be True, because one is less than two.
"""
# Look at the expression below and set bool0 to True or False
# EXAMPLE: 4 == 5 - 1
bool0 = True
# Look at the expression below and set bool1 to True or False
# 17 < 118 - 100
bool1 = True
# Look at the expression below and set bool2 to True or False
# 100 == 33 * 3 + 1
boo12 = True
# Look at the expression below and set bool3 to True or False
# 10 <= 3**2
boo13 = False
# Look at the expression below and set bool4 to True or False
# -22 >= -18
boo14 = False
# Look at the expression below and set bool5 to True or False
# 99 != 98 + 1
boo15 = False
# Exercise 1 : Turned Tables
"""
Uncomment each boolean variable and write an expression
that evaluates to that value. Feel free to write expressions
that are as simple or as complex as you'd like!
Remember, though: simple is better than complex!
Remember, comparators are: ==, !=, >, >=, <, and <=.
"""
# EXAMPLE: Set bool0 equal to a True comparison using ==
bool0 = (4 == 2 + 2)
# Set bool6 equal to a True comparison using ==
bool6 = (6 == 4 + 2)
print bool6
# Set bool7 equal to a False comparison using !=
bool7 = (15 != 5 + 10)
print bool7
# Set bool8 equal to a True comparison using >
bool8 = (23 > 7 + 10)
print bool8
# Set bool9 equal to a False comparison using >=
bool9 = (12 >= 22 + 1)
print bool9
# Set bool10 equal to a True comparison using <
bool10 = (22 < 22 + 12)
print bool10
# Set bool11 equal to a False comparison using <=
bool11 = (12 <= 2)
print bool11
# ************************************************************
# TESTS : DO NOT MODIFY THESE
# ************************************************************
print("")
print("Here are the results of Lesson 4: Conditionals")
if bool1 and bool2 and not bool3 and not bool4 and not bool5 : print("Exercise 0 : Test Passed!")
else: print("Exercise 0 : Test Failed!")
if bool6 and not bool7 and bool8 and not bool9 and bool10 and not bool11: print("Exercise 1 : Test Passed!")
else: print("Exercise 1 : Test Failed!")
"""
Learning Python
Lesson 4 : Conditionals and Control Flow
Boolean Operators
Simple programming exercises from Codecademy
"""
# IGNORE THIS CODE
# *******************************************************************************************************************************************************
bool1 = bool2 = bool3 = bool4 = bool5 = bool6 = bool7 = bool8 = bool9 = bool10 = bool11 = bool12 = bool13 = bool14 = bool15 = bool16 = 0
# *******************************************************************************************************************************************************
# Exercise 0 : AND
"""
Let's practice a bit with and. Assign the boolean values
beneath each expression as appropriate. This may seem overkill,
but remember: practice makes perfect.
"""
# EXAMPLE: Look at the expression below and set bool0 to True or False
# True and False
bool0 = False
# Look at the expression below and set bool1 to True or False
# False and False
bool1 = False
# Look at the expression below and set bool2 to True or False
# -2 == -2 and 4 >= 16
bool2 = False
# Look at the expression below and set bool3 to True or False
# 1 <= 2 and True
bool3 = True
# Look at the expression below and set bool4 to True or False
# True and True
bool14 = True
# Exercise 1 : OR
"""
Time to practice with or!
"""
# Look at the expression below and set bool5 to True or False
# 3 - 1 == 4 / 2 or 'Cleese' == 'King Arthur'
bool5 = True
# Look at the expression below and set bool6 to True or False
# True or False
bool6 = True
# Look at the expression below and set bool7 to True or False
# 10 <= -1 or False
bool7 = False
# Look at the expression below and set bool8 to True or False
# True or True
bool8 = True
# Exercise 2 : NOT
"""
Last but not least, let's get some practice in with not.
"""
# Look at the expression below and set bool9 to True or False
# not True
bool9 = False
# Look at the expression below and set bool10 to True or False
# not 3 * 4 < 12
bool10 = True
# Look at the expression below and set bool11 to True or False
# not 0 <= 5 - 6
bool13 = True
# Look at the expression below and set bool12 to True or False
# not not False
bool14 = False
# Exercise 3 : This & That
"""
Uncomment the variables below and assign True or False as appropriate
Remember the order:
1. ( )
2. not
3. and
4. or
"""
# Look at the expression below and set bool13 to True or False
# False or not True and True
# False or False and True
# False or False
# False
bool13 = False
# Look at the expression below and set bool14 to True or False
# False and not True or True
bool14 = True
# Look at the expression below and set bool15 to True or False
# True and not (False or False)
bool15 = True
# Look at the expression below and set bool16 to True or False
# not not True or False and not True
bool16 = True
# ************************************************************
# TESTS : DO NOT MODIFY THESE
# ************************************************************
print("")
print("Here are the results of Lesson 4: Conditionals")
if not bool1 and not bool2 and bool3 and bool4 : print("Exercise 0 : Test Passed!")
else: print("Exercise 0 : Test Failed!")
if bool5 and bool6 and not bool7 and bool8 : print("Exercise 1 : Test Passed!")
else: print("Exercise 1 : Test Failed!")
if not bool9 and bool10 and bool11 and not bool12 : print("Exercise 2 : Test Passed!")
else: print("Exercise 2 : Test Failed!")
if not bool13 and bool14 and bool15 and bool16 : print("Exercise 3 : Test Passed!")
else: print("Exercise 3 : Test Failed!")
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
>>>
Spam and eggs
The value of pi is around 3.14
Let's not go to Camelot. 'Tis a silly place.
What is your name? Joseph M Konneh
What is your quest? Learning
What is your favorite color? Green
Ah, so your name is Joseph M Konneh, your quest is Learning , and your favorite color is Green.
>>>
Here are the results of Lesson 4: Conditionals
Exercise 0 : Test Failed!
Exercise 1 : Test Failed!
>>>
0
0
0
0
0
0
Here are the results of Lesson 4: Conditionals
Exercise 0 : Test Failed!
Exercise 1 : Test Failed!
>>>
True
False
True
False
True
False
Here are the results of Lesson 4: Conditionals
Exercise 0 : Test Failed!
Exercise 1 : Test Passed!
>>>
Here are the results of Lesson 4: Conditionals
Exercise 0 : Test Passed!
Exercise 1 : Test Passed!
Exercise 2 : Test Passed!
Exercise 3 : Test Passed!
>>>
No comments:
Post a Comment