DEAF 310 Homework 3.1 to 3.6 Solution
Write the definition of a function named sum_list that has one parameter, a
list whose elements are of type int. The function returns the sum of the
elements of the list as an int. --correct answers-- "Write the definition of
a function named sum_list that has one parameter, a list whose elements
are of type int"- will be using "def", and it will have one parameter- x, which
is a list- def sum_list(x):
"The function returns the sum of the elements of the list as an int."- This
means the return value will be the sum of everything in the list. The easiest
way to do this will be using the sum() function
code:
def sum_list(x):
return sum(x)
how to write booleans in python --correct answers-- "True" and "False"-
make sure to capitalize!!!!
how to write does not equal sign --correct answers-- !=
"Write an expression that evaluates to True if and only if is_a_member is
False." --correct answers-- *no equals sign*, but can use the ==
because this just means that something equals a certain value, won't
assign it a value
so the code is:
is_a_member == False
Assume that isIsosceles is a boolean variable , and that the variables
isoCount,triangleCount, and polygonCount have all been initialized. Write a
statement that adds 1 to each of these count variables
(isoCount,triangleCount, and polygonCount)if isIsosceles is true." --correct
answers-- if isIsosceles == True:
isoCount +=1
triangleCount += 1
, polygonCount +=1
"Clunker Motors Inc. is recalling all vehicles from model years 1995-1998
and 2004-2006. Given a variable modelYear write a statement that prints
the message "RECALL" to standard output if the value of modelYear falls
within those two ranges." --correct answers-- if
((1995<=modelYear<=1998) or (2004<=modelYear<=2006)):
print("RECALL")
Given a String variable named sentence that has been initialized, write an
expression whose value is the index of the very last character in the String
referred to by sentence --correct answers-- how to work through this:
"Given a String variable named sentence that has been initialized" - will be
using a variable called "sentence" that is a string
"write an expression" - no equals sign
"the index of the very last character in the String referred to by sentence" -
means that they want the numerical value (place) of the last character
this can be done by finding the length of the entire string, because the
length of the entire string = numerical place of the last character
code = len(sentence[:-1])
does [-1] include the last digit --correct answers-- yes
what does "index" mean --correct answers-- the numerical place of a
letter/number in a string, as you would write it using the indexing operator
ex. index of the letter "r" in "Blair" is 4, because you would do Blair[4] to find
"r"
Write an expression whose value is the character at index 3 of the str
associated with s. --correct answers-- s[3]
Given a variable word that has been assigned a string value, write a string
expression that parenthesizes the value of word. So, if word contains
"sadly", the value of the expression would be the string "(sadly)" --correct
answers-- how to work through this:
the variable "word" contains a string expression in it, and we want to add
quotes and a parenthesis around it. You would think that you would just
add the parenthesis and quotes by using the ""- but no.
Write the definition of a function named sum_list that has one parameter, a
list whose elements are of type int. The function returns the sum of the
elements of the list as an int. --correct answers-- "Write the definition of
a function named sum_list that has one parameter, a list whose elements
are of type int"- will be using "def", and it will have one parameter- x, which
is a list- def sum_list(x):
"The function returns the sum of the elements of the list as an int."- This
means the return value will be the sum of everything in the list. The easiest
way to do this will be using the sum() function
code:
def sum_list(x):
return sum(x)
how to write booleans in python --correct answers-- "True" and "False"-
make sure to capitalize!!!!
how to write does not equal sign --correct answers-- !=
"Write an expression that evaluates to True if and only if is_a_member is
False." --correct answers-- *no equals sign*, but can use the ==
because this just means that something equals a certain value, won't
assign it a value
so the code is:
is_a_member == False
Assume that isIsosceles is a boolean variable , and that the variables
isoCount,triangleCount, and polygonCount have all been initialized. Write a
statement that adds 1 to each of these count variables
(isoCount,triangleCount, and polygonCount)if isIsosceles is true." --correct
answers-- if isIsosceles == True:
isoCount +=1
triangleCount += 1
, polygonCount +=1
"Clunker Motors Inc. is recalling all vehicles from model years 1995-1998
and 2004-2006. Given a variable modelYear write a statement that prints
the message "RECALL" to standard output if the value of modelYear falls
within those two ranges." --correct answers-- if
((1995<=modelYear<=1998) or (2004<=modelYear<=2006)):
print("RECALL")
Given a String variable named sentence that has been initialized, write an
expression whose value is the index of the very last character in the String
referred to by sentence --correct answers-- how to work through this:
"Given a String variable named sentence that has been initialized" - will be
using a variable called "sentence" that is a string
"write an expression" - no equals sign
"the index of the very last character in the String referred to by sentence" -
means that they want the numerical value (place) of the last character
this can be done by finding the length of the entire string, because the
length of the entire string = numerical place of the last character
code = len(sentence[:-1])
does [-1] include the last digit --correct answers-- yes
what does "index" mean --correct answers-- the numerical place of a
letter/number in a string, as you would write it using the indexing operator
ex. index of the letter "r" in "Blair" is 4, because you would do Blair[4] to find
"r"
Write an expression whose value is the character at index 3 of the str
associated with s. --correct answers-- s[3]
Given a variable word that has been assigned a string value, write a string
expression that parenthesizes the value of word. So, if word contains
"sadly", the value of the expression would be the string "(sadly)" --correct
answers-- how to work through this:
the variable "word" contains a string expression in it, and we want to add
quotes and a parenthesis around it. You would think that you would just
add the parenthesis and quotes by using the ""- but no.