Chapter 4
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.
if isIsosceles == True: isoCount += 1 triangleCount += 1 polygonCount += 1
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 .
on_off_switch= not(on_off_switch)
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!
...
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.
...
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...
...
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.
workedOvertime= hoursWorked > 40
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!
...
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.
c == " "
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 .
first=max(name1, name2)
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.
if sold_yesterday > sold_today: sales_trend = -1 else: sales_trend = 1
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.
if temperature > 98.6: fever = True
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.
isQuadrilateral = numberOfSides==4
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.
number_of_men >= number_of_women
Write an expression that evaluates to True if and only if the value of x is equal to zero.
x == 0
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!
...
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!
...
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.
...
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.
...
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...
...
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.
...
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 .
...
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.
age < 19 or is_full_time_student == True
Given a variable called average , write an expression that is True if and only if average is less than 60.5 .
average < 60.5
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.
c != str(" ")
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.
c== "\t"
Given an intvariable gross_pay , write an expression that evaluates to True if and only if gross_pay is less than 10,000.
gross_pay < 10000
Assign true to the variable hasPassedTest .
hasPassedTest = True
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.
hours_worked > 40
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.
if age < 18: minors += 1 elif age < 64: adults += 1 else: age > 65 seniors+=1
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.
if age >= 65: senior_citizens += 1 else: non_seniors += 1
Write a conditional that multiplies the value associated with pay by one-and-a-half if worked_overtime is associated with True .
if outside_temperature > 90: shelf_life -= 4
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
if ph < 7: neutral = 0 base = 0 acid = 1 elif ph > 7: neutral = 0 base = 1 acid = 0 else: neutral = 1 base = 0
Write an if/else statement that assigns True to fever if temperature is greater than 98.6; otherwise it assigns False to fever .
if temperature > 98.6: fever = True else: fever=False
Write a conditional that assigns True to fever if temperature is greater than 98.6.
if temperature > 98.6: fever = True
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"!)
if worked_overtime== True: pay *= 1.5
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
if x > y: max=x else: max=y
Write an expression that evaluates to True if the value of index is greater than the value of last_index .
index > last_index
Write an expression that evaluates to True if and only if is_a_member is False .
is_a_member== False
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.
is_empty or number_of_credits == 3
Write an expression that evaluates to True if and only if the variables profits and losses are exactly equal.
profits==losses
Write an expression that evaluates to True if and only if the bool associated with worked_overtime is True .
worked_overtime== True
Write an expression that evaluates to True if and only if the int associated with x is greater than that associated with y .
x > y
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.
x >= 0 or y < 0
Write an expression that evaluates to True if x is greater than or equal to y .
x >= y
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...
x>= 0 and y < 0