Unit 3 quiz
Function
A set of commands which can be run by calling it by name
r = int(input("Enter the red: ")) g = int(input("Enter the green: ")) b = int(input("Enter the blue: ")) if(r <= 0 or r >= 255): print("Red number is not correct.") if(g <= 0 or g >= 255): print("Green number is not correct.") if(b <= 0 or b >= 255): print("Blue number is not correct.")
Ask the user to enter a number for red, green and blue components of a RGB value. Test to make sure each value is between 0 and 255 inclusive. If a color's value is out of range, print which component is not correct (e.g. Red number is not correct if the red value is 300). Multiple colors may be out of range.
If
Command that tests a true/false condition
Min and Max
Functions in Python that can find the minimum or maximum of a list of numbers
Parameters
Information sent to a function
grade = int(input("What grade are you in? ")) if (grade == 9): print("Freshman") elif( grade == 10): print("Sophomore") elif( grade == 11): print("Junior") elif( grade == 12): print("Senior") else: print("Not in High School")
Input a grade number (9 - 12) and print Freshman, Sophomore, Junior, Senior. If it is not in [9-12], print Not in High School.
color = input("What color? ") if (color == "yellow"): print("Correct!") else: print("Nope")
Input a word. If it is "yellow" print "Correct", otherwise print "Nope". What happens if you type in YELLOW? YellOW? Does the capitalizing make a difference?
Boolean
Keyword used to join test conditions in if-statements
-Bob should be "Bob" -The : is missing -IF should be if
Mark all the mistakes in the following code: IF (name1 == Bob) Bob should be "Bob" The : is missing There should not be ( ) IF should be if
Nested Ifs
Putting one if statement inside another
not
Takes the opposite
day = int(input("Enter today's day numerically: ")) if (day == 15 or day== 30): print ("Its payday!") if (day != 15 and day != 30): print ("Sorry, not a payday.")
Test if a date is payday based on the day of the month(15th or the 30th).
n = float(input("85 ")) if (n > 85): print("Great!") if (n< 85): print("Great!")
Test if a number grade is an A (greater than or equal to 90). If so print "Great!".
password = input("Enter the password: ") if (password == "Ada Lovelace"): print("Correct!") if (password != "Ada Lovelace"): print("Not Correct!")
Test if a password entered is correct. The secret phrase is Ada Lovelace.
Elif
Tests a second (or third, or fourth...) condition in an if statement
or
Tests if at least one condition is true
and
Tests if both conditions are true
not
The following Venn diagram could represent which of the following Boolean statements? and not or
>=
The following code is intended to test if x is NOT less than 17. Fill in the correct symbol: if (x ____ 17):
if (50 > x and x > 40):
The following code should be rewritten as if (50 > x > 40) The code is fine if (50 > x or x > 40): if (50 > x and > 40): if (50 > x and x > 40):
if (x >= -10 and x <= 10):
The following code will not compile â€" which of the following is correct? if (x >= -10 and <= 10): print ("In range") if (x >= -10 and x <= 10): if (x >= -10 AND <= 10): if( (-10 <= x <= 10): if (x >= -10 AND x <= 10):
Syntax
The set of rules that guide what is correct code for the compiler to run
Relationship Operators
The symbols used to compare values in true/false tests
and
To test if a variable is between two numbers you would use: or else ! and
Else
Used to let the program choose between two true/false options, happens when the if statement is false
-Select between options in a program -Make True/ False decisions
What is an IF statement used for? Select all that apply. Repeat sections of code Select between options in a program To count user input Make True/ False decisions
3
What is output? x = 21 if (x > 21): print (1) elif (x < 21): print (2) else: print (3)
ONE
What is the output? x = 9 % 2 if (x == 1): print ("ONE") else: print ("TWO")
if (x == 90 or x == 100):
Which of the following correctly tests if a number equals 90 or 100? if (x == 90 and x == 100): if (x == 90 and 100): if (x == 90 or 100): if (x == 90 or x == 100):
Random
Which of the following is NOT a function? random randint print
b >= a or a > b
Which of the following is true? a = 15 b = 10 b >= a or a > b a < b or a == b a < b and a != b a < b and a == b
num = int(input("Numerator: ")) den = int(input("Denominator: ")) if(den == 0): print("Error - cannot divide by zero.") else: print(num/den)
Write a program to convert a fraction to a decimal. Have your program ask for the numerator first, then denominator. Make sure to check if the denominator is zero.
n = float(input("Enter a number: ")) if (n>45.6): print("Greater than 45.6") if (n<45.6): print("Less than 45.6")
Write a program to enter a number and test if it is greater than 45.6
if (num1 == num2)
Write the code to test if num1 and num2 are the same. if (num1 == num2) if (num1 = num2) if (num1 < num2) if (num1 != num2)
here is an error, the else should be after the elif
color = input("What color? ") if (color == "green"): print ("Correct") else: print ("Nope") elif (color == "red"): print ("Correct) What happens when it is run? There is an error, the else should be after the elif There is an error, the == should be = There is an error, the else should not have a : The code runs as intended
First half of the month
if (month ==9): if(day > 15): print ("Second half of the month") else: print ("First half of the month") else: print("Not in September") What is the output if month = 9 and day = 14? Second half of the month First half of the month Not in September Nothing is output.
-The print (OK) should be print ("OK") -AND should be and
if (x > 5 AND x <= 10): print (OK) Click all the mistakes that apply: The print line should not be indented The print (OK) should be print ("OK") if should be IF AND should be and
A
num = int(input("Enter a number: ")) num = num % 4 if (num == 1): print ("A") elif (num == 2): print ("B") elif (num == 3): print ("C") elif (num == 4): print ("D") else: print ("E") If the user enters 5 what is output? _____________
A C D
x = int (input ("Enter a number: ")) if (x != 8): print ("A") if (x >= 10): print ("B") if (x < 10): print ("C") if (x % 2 == 1): print ("D") What is output if the user types in 9? Click all that apply. A B C D
x >= 0 or x % 2 == 0
x = int(input("Input an integer: ")) if (x >= 0): print("Yay!") else: print("Boo!") It outputs "Yay!" if the value is positive and "Boo!" if the value is negative. Change the condition, so that it only outputs "Yay!" if the value is non-negative (i.e. zero or positive) and even. x >= 0 or x % 2 == 0 x >= 0 and x % 2 == 1 x >= 0 and x % 2 == 0 x >= 0 or x % 2 == 1
function
x = random.randint (1, 100) Â The randint is a ____________. parameter class function variable