If.
Learning Python
Lesson 4 : Conditionals and Control Flow
If, Else and Elif
Simple programming exercises from Codecademy
"""
# Exercise 0 : Conditional Statement Syntax
"""
What do you think will happen in the 3 lines of code below?
"""
answer = "Left"
if answer == "Left":
print "You are correct!"
# Exercise 1 : Create Your Own Conditional Statements
"""
Uncomment the code below and write
an if statement that returns True.
Then print the result of the function like this:
print true_function()
"""
def true_function():
if 5 == 4 + 1 : # Fill in your if statement here!
return True # Make sure this returns True
print true_function()
# Exercise 2 : Else Problems
"""
Complete the if/else statements below.
When you're done print the result of black_night()
and then print the result of french_soldier()
"""
answer = "'Tis but a scratch!"
def black_knight():
if answer == "'Tis but a scratch!":
return True # Make sure this returns True
else:
return False # Make sure this returns False
def french_soldier():
if answer == "Go away, or I shall taunt you a second time!":
return True # Make sure this returns True
else:
return False # Make sure this returns False
print french_soldier()
# Exercise 3 : Elif Problems
"""
Fill in the elif statement so that it
returns True for the answer "I like Spam!"
"""
answer = "I like Spam!"
def feelings_about_spam():
if answer == "I hate Spam!":
return False
elif answer == "I like Spam!": # Write a statement to check the answer variable
return True # Make sure this returns True
else:
return False # Make sure this returns False
# ************************************************************
# TESTS : DO NOT MODIFY THESE
# ************************************************************
ANSWER ARE FOUND BELOW:
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 ====
>>>
You are correct!
True
>>>
You are correct!
True
False
>>>
You are correct!
True
False
>>>
You are correct!
True
False
>>>
"""
Learning Python
Lesson 4 : Conditionals and Control Flow
English to Pig Latin Translator
This program will tranlates English words typed in by the user
and will translate them into Pig Latin
"""
# Exercise 0 : Welcome!
"""
Write a print statement that says "Welcome to the English to Pig Latin translator!"
"""
print "Welcome to the English to Pig Latin translator!"
# Exercise 1 : User Input
"""
Now we need to get input from the user. We can do it like this:
var = raw_input("Insert message here...")
Create your own user input statement.
Instead of "var", name your variable "original"
and make the message say "Please input an English word: "
"""
original = raw_input ("Please input an English word: ")
print original
# Exercise 2 : Check the input
"""
Write an if statement that checks to see if the string is not empty.
If the string is not empty, print the user's word.
Otherwise (else), print "empty" if the string is empty.
HINT: You can check to see if the string is empty by getting its length
For example, len("HELLO") is 5
len("") is 0
When you think you've got it, SAVE and RUN your program and test it out!
"""
pyg = "ay"
if len(original) > 0 and original.isalpha():
word = original.lower()
first = word[0]
if first == "a" or first == "e" or first == "i" or first == "o" or first == "u":
print "vowel"
else:
print "consonant"
else:
print "empty"
# Exercise 3: More checking
"""
Pig Latin only works with words, so we need to check if the user
typed in a word or something like 238asdf23819.
Thankfully, Python can check for this using the isalpha() function.
Modify your if statement to look like this and test it out:
if len(original) > 0 and original.isalpha():
Make sure you SAVE, RUN and test it out before moving on.
"""
# Exercise 4: Adding the "ay"
"""
Write the following code BEFORE your if statement:
Create a variable named pyg and set it equal 'ay'.
Convert what's stored in the variable 'original' to
all lowercase letters and store it as the variable 'word'
Create a new variable called "first" and set it to the first letter of
the variable "word"
"""
# Exercise 5: Checking for Vowels
'''
If the user's word starts with a vowel (a,e,i,o,u), you just add "ay" to the end
For example, "apple" becomes "appleay"
If the user's word starts with anything else, you move the first letter of the word
to the end and add "ay"
For example, "banana" becomes "ananabay"
Let's check to see if the users word starts with a vowel.
Add a new if/else block nested inside of your existing one.
The new if should check to see if the first letter of word is a vowel.
If it is, your code should print "vowel".
If the first letter of word is not a vowel,
your code should print "consonant".
You can remove the "print original" line from your code.
'''
THE CLASS STOPPED THIS LESSON TO LESSON 6
# Exercise 6: appleay!
'''
Now it's time to translate a little bit.
If the word typed by the user is a vowel, translating is easy!
Create a variable called 'new_word' that contains the result of
appending "ay" to the end of the word.
Remember you created a pyg variable that stores the "ay" string
Replace the "print 'vowel'" text and print the 'new_word' variable instead
'''
# Exercise 7: ogday!
'''
Inside the else part of your if/else block that checks the first letter of the word,
set the new_word variable equal to the translation result for a word that starts with a consonant.
Remember, you want to take the whole word (without the first letter) and append the first letter
to the end, and then append "ay" to the end of that.
Create a variable called rest and set it to the rest of the word (without the first letter)
To get the whole word without the first letter, you can slice it using the pattern s[i:j].
For example, if s = "foo", then s[1] gives you "oo"
Replace the print 'consonant' bit with print new_word. Make sure to test your code
with a word that starts with a consonant!
'''
# Exercise 8: Testing, testing!
'''
Make sure you test your program several times and use different kinds of input.
You never know what people will type, so try lots of things (numbers, letters, symbols)
to make sure that it works properly!
'''
ANSWER ARE FOUND BELOW TOO:
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 ====
>>>
You are correct!
True
>>>
You are correct!
True
False
>>>
You are correct!
True
False
>>>
You are correct!
True
False
>>>
Welcome to the English to Pig Latin translator!
>>>
Welcome to the English to Pig Latin translator!
You will enjoy the Pig Latin translator
>>>
Welcome to the English to Pig Latin translator!
Please input an English word: Apple
Apple
Apple
>>>
Welcome to the English to Pig Latin translator!
Please input an English word: 1212121
1212121
empty
>>>
Welcome to the English to Pig Latin translator!
Please input an English word: Apple
Apple
Apple
>>>
Welcome to the English to Pig Latin translator!
Please input an English word: Apple
Apple
Apple
Welcome to the English to Pig Latin translator!
Please input an English word: Air
Air
Air
Welcome to the English to Pig Latin translator!
Please input an English word: Air
Air
Air
vowel
>>>
Welcome to the English to Pig Latin translator!
Please input an English word: Bird
Bird
consonant
>>>
- Test a condition to see if it's true
- if age <18:
- if name == "Zane":
- Only one of these per if/elif/else
Learning Python
Lesson 4 : Conditionals and Control Flow
If, Else and Elif
Simple programming exercises from Codecademy
"""
# Exercise 0 : Conditional Statement Syntax
"""
What do you think will happen in the 3 lines of code below?
"""
answer = "Left"
if answer == "Left":
print "You are correct!"
# Exercise 1 : Create Your Own Conditional Statements
"""
Uncomment the code below and write
an if statement that returns True.
Then print the result of the function like this:
print true_function()
"""
def true_function():
if 5 == 4 + 1 : # Fill in your if statement here!
return True # Make sure this returns True
print true_function()
# Exercise 2 : Else Problems
"""
Complete the if/else statements below.
When you're done print the result of black_night()
and then print the result of french_soldier()
"""
answer = "'Tis but a scratch!"
def black_knight():
if answer == "'Tis but a scratch!":
return True # Make sure this returns True
else:
return False # Make sure this returns False
def french_soldier():
if answer == "Go away, or I shall taunt you a second time!":
return True # Make sure this returns True
else:
return False # Make sure this returns False
print french_soldier()
# Exercise 3 : Elif Problems
"""
Fill in the elif statement so that it
returns True for the answer "I like Spam!"
"""
answer = "I like Spam!"
def feelings_about_spam():
if answer == "I hate Spam!":
return False
elif answer == "I like Spam!": # Write a statement to check the answer variable
return True # Make sure this returns True
else:
return False # Make sure this returns False
# ************************************************************
# TESTS : DO NOT MODIFY THESE
# ************************************************************
ANSWER ARE FOUND BELOW:
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 ====
>>>
You are correct!
True
>>>
You are correct!
True
False
>>>
You are correct!
True
False
>>>
You are correct!
True
False
>>>
"""
Learning Python
Lesson 4 : Conditionals and Control Flow
English to Pig Latin Translator
This program will tranlates English words typed in by the user
and will translate them into Pig Latin
"""
# Exercise 0 : Welcome!
"""
Write a print statement that says "Welcome to the English to Pig Latin translator!"
"""
print "Welcome to the English to Pig Latin translator!"
# Exercise 1 : User Input
"""
Now we need to get input from the user. We can do it like this:
var = raw_input("Insert message here...")
Create your own user input statement.
Instead of "var", name your variable "original"
and make the message say "Please input an English word: "
"""
original = raw_input ("Please input an English word: ")
print original
# Exercise 2 : Check the input
"""
Write an if statement that checks to see if the string is not empty.
If the string is not empty, print the user's word.
Otherwise (else), print "empty" if the string is empty.
HINT: You can check to see if the string is empty by getting its length
For example, len("HELLO") is 5
len("") is 0
When you think you've got it, SAVE and RUN your program and test it out!
"""
pyg = "ay"
if len(original) > 0 and original.isalpha():
word = original.lower()
first = word[0]
if first == "a" or first == "e" or first == "i" or first == "o" or first == "u":
print "vowel"
else:
print "consonant"
else:
print "empty"
# Exercise 3: More checking
"""
Pig Latin only works with words, so we need to check if the user
typed in a word or something like 238asdf23819.
Thankfully, Python can check for this using the isalpha() function.
Modify your if statement to look like this and test it out:
if len(original) > 0 and original.isalpha():
Make sure you SAVE, RUN and test it out before moving on.
"""
# Exercise 4: Adding the "ay"
"""
Write the following code BEFORE your if statement:
Create a variable named pyg and set it equal 'ay'.
Convert what's stored in the variable 'original' to
all lowercase letters and store it as the variable 'word'
Create a new variable called "first" and set it to the first letter of
the variable "word"
"""
# Exercise 5: Checking for Vowels
'''
If the user's word starts with a vowel (a,e,i,o,u), you just add "ay" to the end
For example, "apple" becomes "appleay"
If the user's word starts with anything else, you move the first letter of the word
to the end and add "ay"
For example, "banana" becomes "ananabay"
Let's check to see if the users word starts with a vowel.
Add a new if/else block nested inside of your existing one.
The new if should check to see if the first letter of word is a vowel.
If it is, your code should print "vowel".
If the first letter of word is not a vowel,
your code should print "consonant".
You can remove the "print original" line from your code.
'''
THE CLASS STOPPED THIS LESSON TO LESSON 6
# Exercise 6: appleay!
'''
Now it's time to translate a little bit.
If the word typed by the user is a vowel, translating is easy!
Create a variable called 'new_word' that contains the result of
appending "ay" to the end of the word.
Remember you created a pyg variable that stores the "ay" string
Replace the "print 'vowel'" text and print the 'new_word' variable instead
'''
# Exercise 7: ogday!
'''
Inside the else part of your if/else block that checks the first letter of the word,
set the new_word variable equal to the translation result for a word that starts with a consonant.
Remember, you want to take the whole word (without the first letter) and append the first letter
to the end, and then append "ay" to the end of that.
Create a variable called rest and set it to the rest of the word (without the first letter)
To get the whole word without the first letter, you can slice it using the pattern s[i:j].
For example, if s = "foo", then s[1] gives you "oo"
Replace the print 'consonant' bit with print new_word. Make sure to test your code
with a word that starts with a consonant!
'''
# Exercise 8: Testing, testing!
'''
Make sure you test your program several times and use different kinds of input.
You never know what people will type, so try lots of things (numbers, letters, symbols)
to make sure that it works properly!
'''
ANSWER ARE FOUND BELOW TOO:
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 ====
>>>
You are correct!
True
>>>
You are correct!
True
False
>>>
You are correct!
True
False
>>>
You are correct!
True
False
>>>
Welcome to the English to Pig Latin translator!
>>>
Welcome to the English to Pig Latin translator!
You will enjoy the Pig Latin translator
>>>
Welcome to the English to Pig Latin translator!
Please input an English word: Apple
Apple
Apple
>>>
Welcome to the English to Pig Latin translator!
Please input an English word: 1212121
1212121
empty
>>>
Welcome to the English to Pig Latin translator!
Please input an English word: Apple
Apple
Apple
>>>
Welcome to the English to Pig Latin translator!
Please input an English word: Apple
Apple
Apple
Welcome to the English to Pig Latin translator!
Please input an English word: Air
Air
Air
Welcome to the English to Pig Latin translator!
Please input an English word: Air
Air
Air
vowel
>>>
Welcome to the English to Pig Latin translator!
Please input an English word: Bird
Bird
consonant
>>>
No comments:
Post a Comment