I2P – Python Lesson 4 (Part 2) – Reflection

Lesson 4 – Code:

##IF ELSE
vScore = 75
#\n = new paragraph
if vScore >= 50:
    print("\nPASS")
else:
    print("\nFAIL")


##IF IN
vMonth = "September"
vLetter = "e"
if vLetter in vMonth:
    print("There is a letter, ", vLetter, ",in", vMonth)
else:
    print("There is NOT a letter", vLetter, "in", vMonth)

vLength = len(vMonth)
print("There are",vLength,"letters in",vMonth)

vChoice = input("Enter Number 1 to 3:")
print()

if vChoice =="1":
    print("Chosen Item 1")

elif vChoice == "2":
    print("Chosen Item 2")

elif vChoice == "3":
    print("Chosen Item 3")
#some unknown value has been entered
else:
    print("Sorry, but", vChoice, "isn't a valid Number,")

input("\n\nPress the enter key to exit")
#"if" is used for the first choice, and elif is used for the rest. For example, if you input 2 for vChoice, then it prints "Chosen Item 2"

Lesson 4 – Code Result: 

Screen Shot 2016-10-25 at 7.46.15 PM

Vowel Score Calculator Code: 

#program that works out the score for a given word
#a = 5
#e = 4
#i = 3
#o = 2
#u = 1
vWord = input("Enter a word: ")

#Convert the word into lower case
vWord = vWord.lower()

#Set score to be 0
score = 0
#Create a loop so that
#letters are looked at one by one...
for letter in vWord:
    if letter =="a":
        score = score + 5
print (letter, "is worth 5")
    elif letter =="e":
        score = score + 4
print (letter, "is worth 4")
    elif letter =="i":
        score = score + 3
print (letter, "is worth 3")
    elif letter =="o":
        score = score + 2
print (letter, "is worth 2")
    elif letter =="u":
        score = score + 1
print (letter, "is worth 1")
    else:
        print(letter, "is worth 0")

#an else, in case the letter is not a vowel
#end of loop
#Print the score
print("Your total score is ", score)

Vowel Score Calculator End Product:

Guess the Word Game Code [so far]: 

import random
vChoices = ("red", "gold", "orange", "green", "aquamarine", "lavender", "fuchsia")
vWord = (random.choice(vChoices))
vName=input("Hi there. What's your name?")

vGuess = input("I have one of the following colours in mind:red, gold, orange, green, aquamarine, lavender, fuchsia . Can you guess which colour?")
vAnswer = len(vWord)
vGuessNumber = len(vGuess)
while vGuess != vWord:
   if vGuessNumber > vAnswer:
       print("This word has less letters than your guess")
       vGuess = input("Try again!")
   elif vGuessNumber < vAnswer:
       print("This word has more letters than your guess")
       vGuess = input("Try again!")
   elif vGuessNumber == vAnswer:
       print("You got it! Well done,", vName, "!")

Pseudo Code for Guess the Word Game:

*Each of the colours have a distinct number of characters

Choose a random word from the list given of the words below.

Set vWord as the variable for the chosen random word.

Set vName as input for “Hi there. What’s your name?”

Set vChoices is equal to the list of words that the computer is going to choose from

Set vGuess as the input for the question “I have one of the following colours in mind: red, gold, orange, green, aquamarine, lavender, fuchsia. Can you guess which colour?”

Set vAnswer as the length of vWord

Set vGuessNumber as length of vGuess

(Create a while loop that encompasses three “if” statements)

While vGuess is not equal to vAnswer:

If vGuessNumber is greater than vAnswer,

Then print “This word has less letters than your guess”

Set vGuess as input for “Try again!”

If vGuessNumber is less than vAnswer,

Then print “This word has more letters than your guess”

Set vGuess as input for “Try again!”

If vGuessNumber is equal to vAnswer,

Then print “You got it! Well done,” vName, “!”

Guess the Word Game Result [so far]: 


In this lesson, we learned about the “If… else” functions and also how the “elif” function plays a part in this. The “if… else” function is used when the condition can either be true or false. If the condition is true, then it follows the ‘if’ statements, and if the condition is false, then the computer follows the “else” statements. For example, in the Vowel Score Calculator Code, the computer reads the “for..in” loop as if there is the letter “a” in the given word, then add five points to the score and say “[letter] is worth 5 points”. If not, or else, then print “[letter] is worth 0 points]. The “elif” function simply has the same purpose as the “if” function, it’s just used to replace “if” functions after the first “if statement”. In the Vowel Score Calculator Code example, after the first “if” statement with the vowel a, we would use ‘elif’ statements to continue with the rest of the vowels. The flow chart for an ‘if… else’ function would look like this:

if_else_statement

A common question arises in the midst of coding: What is the difference, then, between ‘while’ loops and ‘for… in…’ loops? The answer is that they are very different. ‘while’ loops repeatedly execute the given functions as long as the condition is true, because if the condition is false then the code skips over the while loop and continues with the code. In the Guess the Word Game Code, the while loop is used whenever the length of the answer is not equal to the length of the length of the guess. If it is not equal, meaning if the condition is true for this while loop, then the computer will proceed to the following if functions. If the condition is false and the lengths are equal, then the computer will skip over the loop and say good job, or in this case end the code. While loops look like this:

python_while_loop

On the other hand, for loops have the ability to go over the items of any sequence, such as a list or a string. If the given sequence contains something that is wanted, then the computer continues with the execution statement. If the given sequence does not contain something it wants, then the computer skips over the if loop and continues with the rest of the code. In the example the Vowel Score Calculator Code, the computer reads that for every vowel in the word, the computer must proceed and follow the if statements. In this case, for the consonants, the computer would proceed to the else statement. The for loop looks like this:

for loop in Python

Leave a Reply

Your email address will not be published. Required fields are marked *