Assignment: Week Two >> Python Basics
Write one for loop to print out each character of the string my_str
on a separate line.
my_str = "MICHIGAN"
for i in my_str:
print(i)
M
I
C
H
I
G
A
N
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | ‘for’ | ‘my_st…nt(i)’ | Testing your code (Don’t worry about actual and expected values). |
Pass | ‘M\nI\nC\nH\nI\nG\nA\nN’ | ‘M\nI\nC…\nA\nN\n’ | Testing output (Don’t worry about actual and expected values). |
You passed: 100.0% of the tests
Score: 1.0 / 1
Write one for loop to print out each element of the list several_things
. Then, write another for loop to print out the TYPE of each element of the list several_things
. To complete this problem you should have written two different for loops, each of which iterates over the list several_things
, but each of those 2 for loops should have a different result.
several_things = ["hello", 2, 4, 6.0, 7.5, 234352354, "the end", "", 99]
for i in several_things:
print(i)
for i in several_things:
print(type(i))
hello
2
4
6.0
7.5
234352354
the end
99
<class 'str'>
<class 'int'>
<class 'int'>
<class 'float'>
<class 'float'>
<class 'int'>
<class 'str'>
<class 'str'>
<class 'int'>
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | ‘for’ | ‘sever…e(i))’ | Testing your code (Don’t worry about actual and expected values). |
Pass | True | True | Testing output (Don’t worry about actual and expected values). |
You passed: 100.0% of the tests
Score: 1.0 / 1
Write code that uses iteration to print out the length of each element of the list stored in str_list
.
str_list = ["hello", "", "goodbye", "wonderful", "I love Python"]
# Write your code here.
for i in str_list:
print(len(i))
5
0
7
9
13
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | ‘for’ | ‘str_l…n(i))’ | Testing whether you used a for loop (Don’t worry about actual and expected values). |
Pass | ‘5\n0\n7\n9\n13’ | ‘5\n0\n7\n9\n13\n’ | Testing output (Don’t worry about actual and expected values). |
You passed: 100.0% of the tests
Score: 1.0 / 1
Write code to count the number of characters in original_str
using the accumulation pattern and assign the answer to a variable num_chars
. Do NOT use the len
function to solve the problem (if you use it while you are working on this problem, comment it out afterward!)
original_str = "The quick brown rhino jumped over the extremely lazy fox."
acum=0
for i in original_str:
acum += 1
num_chars = acum
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | 57 | 57 | Testing whether num_chars_sent has the correct value |
Pass | ‘len’ | ‘origi… acum’ | Testing that you are not including the len function in your code. (Don’t worry about Actual and Expected Values.) |
You passed: 100.0% of the tests
Score: 1.0 / 1
addition_str
is a string with a list of numbers separated by the +
sign. Write code that uses the accumulation pattern to take the sum of all of the numbers and assigns it to sum_val
(an integer). (You should use the .split("+")
function to split by "+"
and int()
to cast to an integer).
addition_str = "2+5+10+20"
lst = addition_str.split('+')
new_lst = [int(i) for i in lst]
sum_val = sum(new_lst)
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | 37 | 37 | Testing whether sum_val has the correct value |
Pass | ‘split’ | ‘addit…_lst)’ | Testing your code (Don’t worry about actual and expected values). |
Pass | ‘int’ | ‘addit…_lst)’ | Testing your code (Don’t worry about actual and expected values). |
You passed: 100.0% of the tests
Score: 1.0 / 1
week_temps_f
is a string with a list of fahrenheit temperatures separated by the ,
sign. Write code that uses the accumulation pattern to compute the average (sum divided by number of items) and assigns it to avg_temp
. Do not hard code your answer (i.e., make your code compute both the sum or the number of items in week_temps_f
) (You should use the .split(",")
function to split by ","
and float()
to cast to a float).
week_temps_f = "75.1,77.7,83.2,82.5,81.0,79.5,85.7"
temp_list = week_temps_f.split(',')
lst=[float(i) for i in temp_list]
avg_temp = sum(lst)/len(lst)
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | 80.6714285714 | 80.6714285714 | Testing that avg_temp has the correct value |
Pass | ‘split’ | ‘week_…(lst)’ | Testing your code (Don’t worry about actual and expected values). |
Pass | ‘float’ | ‘week_…(lst)’ | Testing your code (Don’t worry about actual and expected values). |
You passed: 100.0% of the tests
Score: 1.0 / 1
Write code to create a list of numbers from 0 to 67 and assign that list to the variable nums
. Do not hard code the list.
nums = range(68)
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | [0, 1…, 67] | [0, 1…, 67] | Testing that nums is a list that contains the correct elements. |
You passed: 100.0% of the tests
Score: 1.0 / 1
Write code to create a list of word lengths for the words in original_str
using the accumulation pattern and assign the answer to a variable num_words_list
. (You should use the len
function).
original_str = "The quick brown rhino jumped over the extremely lazy fox"
original_lst = original_str.split()
num_words_list = []
for i in original_lst:
num_words_list.append(len(i))
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | [3, 5…4, 3] | [3, 5…4, 3] | Testing whether num_words_list has the correct value |
Pass | ‘for’ | ‘origi…n(i))’ | Testing that you are using a for loop in your code. |
You passed: 100.0% of the tests
Score: 1.0 / 1
Create an empty string and assign it to the variable lett
. Then using range, write code such that when your code is run, lett
has 7 b’s ("bbbbbbb"
).
lett = ""
for i in range(0,7):
lett = lett + 'b'
print(lett)
bbbbbbb
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | ‘bbbbbbb’ | ‘bbbbbbb’ | Testing that lett has the correct value. |
Pass | ‘bbbbbbb’ | ‘lett …lett)’ | Testing that you didn’t hardcode the answer. |
You passed: 100.0% of the tests
Score: 0.0 / 0
Write a program that uses the turtle module and a for loop to draw something. It doesn’t have to be complicated, but draw something different than we have done in the past. (Hint: if you are drawing something complicated, it could get tedious to watch it draw over and over. Try setting .speed(10)
for the turtle to draw fast, or .speed(0)
for it to draw super fast with no animation.)