Chapter 4

7 September 2022
4.7 (114 reviews)
45 test answers

Unlock all answers in this set

Unlock answers (41)
question
Write an expression that evaluates to True if and only if the value of x is equal to zero.
answer
x == 0
question
Write an expression that evaluates to True if and only if the variables profits and losses are exactly equal.
answer
profits==losses
question
Given the variable c , whose associated value is a str , write an expression that is True if and only if c is not equal to a string consisting of a single blank.
answer
c != str(" ")
question
Write an expression that evaluates to True if the value of index is greater than the value of last_index .
answer
index > last_index
question
Working overtime is defined as having worked more than 40 hours during the week. Given the variable hours_worked , write an expression that evaluates to True if the employee worked overtime.
answer
hours_worked > 40
question
Write an expression that evaluates to True if x is greater than or equal to y .
answer
x >= y
question
Given the variables number_of_men and number_of_women , write an expression that evaluates to True if the number of men is greater than or equal to the number of women.
answer
number_of_men >= number_of_women
question
Given a variable called average , write an expression that is True if and only if average is less than 60.5 .
answer
average < 60.5
question
Given an intvariable gross_pay , write an expression that evaluates to True if and only if gross_pay is less than 10,000.
answer
gross_pay < 10000
question
Write an expression that evaluates to True if and only if the int associated with x is greater than that associated with y .
answer
x > y
question
Assume that a variable hoursWorked has been initialized. Write a statement that assigns the value True to the variable workedOvertime if hoursWorked is greater than 40 and False otherwise.
answer
workedOvertime= hoursWorked > 40
question
Assign true to the variable hasPassedTest .
answer
hasPassedTest = True
question
Assume that a variable variable , numberOfSides has been initialized. Write a statement that assigns the value True to the variable isQuadrilateral if numberOfSides is exactly 4 and False otherwise.
answer
isQuadrilateral = numberOfSides==4
question
Assume that c is a variable that has been assigned a string value. Write an expression whose value is true if and only if c is a space character.
answer
c == " "
question
Assume that c is a variable has been assigned a string value. Write an expression whose value is true if and only if c is a tab character.
answer
c== "t"
question
Write a conditional that assigns True to fever if temperature is greater than 98.6.
answer
if temperature > 98.6: fever = True
question
Write a conditional that assigns 10,000 to bonus if the goods_sold is greater than 500,000. Write a conditional that decreases the value associated with shelf_life by 4 if the value associated with outside_temperature is greater than 90.
answer
if temperature > 98.6: fever = True
question
Write a conditional that multiplies the value associated with pay by one-and-a-half if worked_overtime is associated with True .
answer
if outside_temperature > 90: shelf_life -= 4
question
Given the variables name1 and name2 , write a fragment of code that assigns the larger of their associated values to first . (NOTE: "larger" here means alphabetically larger, not "longer". Thus, "mouse" is larger than "elephant" because "mouse" comes later in the dictionary than "elephant"!)
answer
if worked_overtime== True: pay *= 1.5
question
Given x and y , each associated with an int , write a fragment of code that associates the larger of these with another variable named max .
answer
first=max(name1, name2)
question
Given the variables x , y , and z , each associated with an int , write a fragment of code that assigns the smallest of these to min
answer
if x > y: max=x else: max=y
question
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.
answer
if isIsosceles == True: isoCount += 1 triangleCount += 1 polygonCount += 1
question
Write an if/else statement that compares age with 65, adds 1 to senior_citizens if age is greater than or equal to 65 , and adds 1 to non_seniors otherwise.
answer
if age >= 65: senior_citizens += 1 else: non_seniors += 1
question
Write an if/else statement that compares sold_yesterday and sold_today , and based upon that comparison assigns sales_trend the value -1 (the case where sold_yesterday is greater than sold_today ) or 1.
answer
if sold_yesterday > sold_today: sales_trend = -1 else: sales_trend = 1
question
Write an if/else statement that assigns True to fever if temperature is greater than 98.6; otherwise it assigns False to fever .
answer
if temperature > 98.6: fever = True else: fever=False
question
Write an if/else statement that adds 1 to minors if age is less than 18, adds 1 to adults if age is 18 through 64 and adds 1 to seniors if age is 65 or older.
answer
if age < 18: minors += 1 elif age < 64: adults += 1 else: age > 65 seniors+=1
question
Given that ph refers to a float , write a statement that compares ph to 7.0 and makes the following assignments (RESPECTIVELY!) to the variables neutral , base , and acid : 0,0,1 if ph is less than 7 0,1,0 if ph is greater than 7 1,0,0 if ph is equal to 7
answer
if ph < 7: neutral = 0 base = 0 acid = 1 elif ph > 7: neutral = 0 base = 1 acid = 0 else: neutral = 1 base = 0
question
Write an expression that evaluates to True if and only if the bool associated with worked_overtime is True .
answer
worked_overtime== True
question
Given the already defined variables is_full_time_student and age , write an expression that evaluates to True if age is less than 19 or is_full_time_student is True.
answer
age < 19 or is_full_time_student == True
question
Write an expression that evaluates to True if and only if is_a_member is False .
answer
is_a_member== False
question
Write a statement that toggles on_off_switch . That is, if on_off_switch is False , it is made equal to True ; if on_off_switch is True , it is made equal to False .
answer
on_off_switch= not(on_off_switch)
question
Given that the variables x and y have been defined, write an expression that evaluates to True if x is non-negative and y is negative. More...
answer
x>= 0 and y < 0
question
Given that the variables x and y have been defined, write an expression that evaluates to True if x is non-negative or y is negative.
answer
x >= 0 or y < 0
question
Given two variables , is_empty which is associated with a bool indicating whether a class roster is empty or not, and number_of_credits which is associated with an int (the number of credits for a class), write an expression that evaluates to True if the class roster is empty or the class is exactly three credits.
answer
is_empty or number_of_credits == 3
question
Write an expression that evaluates to True if the int associated with number_of_prizes is divisible (with no remainder) by the int associated with number_of_participants . Assume that the latter is not zero. More...
answer
...
question
Write an expression that evaluates to True if the int associated with width_of_box is not divisible (with no remainder) by the int associated with width_of_book . Assume that the latter is not zero. Write an expression that evaluates to True if the int associated with width_of_box is not divisible (with no remainder) by the int associated with width_of_book . Assume that the latter is not zero.
answer
...
question
Write an expression that evaluates to True if the value associated with x is even, but False if the value is odd. More... Given the already defined variables temperature and humidity , write an expression that evaluates to True if and only if temperature is greater than 90 and humidity is less than 10 .
answer
...
question
Given the already defined variables years_with_company and department , write an expression that evaluates to True if years_with_company is less than 5 and department is not equal to 99.
answer
...
question
Given two variables , is_empty of type bool , indicating whether a class roster is empty or not, and number_of_credits of type integer , containing the number of credits for a class, write an expression that evaluates to True if the class roster is not empty and the class is one or three credits. More...
answer
...
question
Given two variables , is_empty which is associated with a bool , indicating whether a class roster is empty or not, and number_of_credits which is associated with an int , containing the number of credits for a class, write an expression that evaluates to True if the class roster is not empty and the class is more than two credits.
answer
...
question
Clunker Motors Inc. is recalling all vehicles in its Extravagant line from model years 1999-2002. Given variables modelYear and modelName write a statement that assigns True to recalled if the values of modelYear and modelName match the recall details and assigns false otherwise.
answer
...
question
Clunker Motors Inc. is recalling all vehicles from model years 1995-1998 and 2004-2006.Given a variable modelYear write a statement that assigns True to recalled if the value of modelYear falls within the two recall ranges and assigns False otherwise. Do not use an if statement in this exercise!
answer
...
question
Clunker Motors Inc. is recalling all vehicles from model years 2001-2006. Given a variable modelYear write a statement that assigns True to recalled if the value of modelYear falls within the recall range and assigns False otherwise. Do not use an if statement in this exercise!
answer
...
question
Clunker Motors Inc. is recalling all vehicles from model years 2001-2006. Given a variable modelYear write a statement that assigns True to norecall if the value of modelYear does NOT within the recall range and assigns False otherwise. Do not use an if statement in this exercise!
answer
...
question
Clunker Motors Inc. is recalling all vehicles from model years 1995-1998 and 2004-2006. Given a variable modelYear write a statement that assigns true to norecall if the value of modelYear does NOT fall within the two recall ranges and assigns false otherwise. Do not use an if statement in this exercise!
answer
...