Assignment: Final Course Assignment >> Python Basics
Below are a set of scores that students have received in the past semester. Write code to determine how many are 90 or above and assign that result to the value a_scores
.
scores = "67 80 90 78 93 20 79 89 96 97 92 88 79 68 58 90 98 100 79 74 83 88 80 86 85 70 90 100"
lst = scores.split()
a_scores = 0
for i in lst:
if int(i) >= 90:
a_scores += 1
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | 10 | 10 | Testing that you got the right count. |
Pass | ‘for’ | ‘score… += 1’ | Testing that you used a for loop. |
Pass | ‘if’ | ‘score… += 1’ | Testing that you used a conditional. |
You passed: 100.0% of the tests
Score: 1.0 / 1
Write code that uses the string stored in org
and creates an acronym which is assigned to the variable acro
. Only the first letter of each word should be used, each letter in the acronym should be a capital letter, and there should be nothing to separate the letters of the acronym. Words that should not be included in the acronym are stored in the list stopwords
. For example, if org
was assigned the string “hello to world” then the resulting acronym should be “HW”.
stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
org = "The organization for health, safety, and education"
acro = ""
lst = org.split()
for i in lst:
if i in stopwords:
lst.remove(i)
for j in lst:
acro += j[0]
acro = acro.upper()
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | ‘OHSE’ | ‘OHSE’ | Checking that acro has been set correctly. |
Pass | True | True | Checking that acro is a string. |
Pass | ‘for’ | ‘stopw…per()’ | Testing that you used a for loop. |
You passed: 100.0% of the tests
Score: 1.0 / 1
Write code that uses the string stored in sent
and creates an acronym which is assigned to the variable acro
. The first two letters of each word should be used, each letter in the acronym should be a capital letter, and each element of the acronym should be separated by a “. ” (dot and space). Words that should not be included in the acronym are stored in the list stopwords
. For example, if sent
was assigned the string “height and ewok wonder” then the resulting acronym should be “HE. EW. WO”.
stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', 'The']
sent = "The water earth and air are vital"
acro = ""
lst = sent.split()
for i in lst:
if i in stopwords:
lst.remove(i)
for j in lst:
acro = acro + j[0] + j[1]
if j != lst[-1]:
acro += ". "
acro = acro.upper()
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | ‘WA. E…R. VI’ | ‘WA. E…R. VI’ | Checking that acro has been set correctly. |
Pass | True | True | Checking that acro is a string. |
Pass | ‘for’ | ‘stopw…per()’ | Testing that you used a for loop. |
You passed: 100.0% of the tests
Score: 1.0 / 1
A palindrome is a phrase that, if reversed, would read the exact same. Write code that checks if p_phrase
is a palindrome by reversing it and then checking if the reversed version is equal to the original. Assign the reversed version of p_phrase
to the variable r_phrase
so that we can check your work.
p_phrase = "was it a car or a cat I saw"
r_phrase = p_phrase[::-1]
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | ‘was I…i saw’ | ‘was I…i saw’ | checking that r_phrase is set correctly |
Pass | ‘wasit…tisaw’ | ‘wasit…tisaw’ | checking that r_phrase and p_phrase are equivalent if the spaces are placed in the correct locations. |
Pass | ‘was i…I saw’ | ‘was I…i saw’ | checking that r_phrase and p_phrase are not the same object. |
You passed: 100.0% of the tests
Score: 1.0 / 1
Provided is a list of data about a store’s inventory where each item in the list represents the name of an item, how much is in stock, and how much it costs. Print out each item in the list with the same formatting, using the .format method (not string concatenation). For example, the first print statment should read The store has 12 shoes, each for 29.99 USD.
inventory = ["shoes, 12, 29.99", "shirts, 20, 9.99", "sweatpants, 25, 15.00", "scarves, 13, 7.75"]
for temp in inventory:
temp = temp.split(',')
str1="The store has{} {}, each for{} USD."
str1=str1.format(temp[1],temp[0],temp[2])
print(str1)
# 2nd way of string formatting
inventory = ["shoes, 12, 29.99", "shirts, 20, 9.99", "sweatpants, 25, 15.00", "scarves, 13, 7.75"]
for temp in inventory:
temp = temp.split(',')
item = temp[0]
quantity = temp[1]
amount = temp[2]
str1="The store has{} {}, each for{} USD."
str1=str1.format(quantity,item,amount)
print(str1)
The store has 12 shoes, each for 29.99 USD.
The store has 20 shirts, each for 9.99 USD.
The store has 25 sweatpants, each for 15.00 USD.
The store has 13 scarves, each for 7.75 USD.
The store has 12 shoes, each for 29.99 USD.
The store has 20 shirts, each for 9.99 USD.
The store has 25 sweatpants, each for 15.00 USD.
The store has 13 scarves, each for 7.75 USD.
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | ‘for’ | ‘inven…str1)’ | Testing whether your code includes a for loop. |
Pass | ‘.format(‘ | ‘inven…str1)’ | Testing whether your code invokes the .format method. |
Pass | ‘The s…USD.\n’ | ‘The s…USD.\n’ | Testing your output. |
You passed: 100.0% of the tests