comp sci - ch. 7-8 test
why do formats including 9 still not line up 8 and 9 digit numbers
9 specifies total number of characters, not j digits, including digits, commas, and decimal
all conditions end with what
:
= vs ==
= - assignment operator == - equality operator
3 types of selection
One-way, two-way, and multiple-way
write program that counts from 1864 to 2091
for k in range(1864,2092):
write program that counts from 2015 to 1885 backwards by 10s
for k in range(2015,1884,-10):
write program that counts from 33 to 99 by 3s
for k in range(33,100,3):
Rewrite program so the program counts up to 40 for k in range(40)
for k in range(41)
write a program that will display a regular octagon
for k in range(8): forward(90) left(315)
write program that displays all times tables from 1 times 1 to 60 times 60 and allow for numbers to be up to 4 digits in length
for r in range(1,61): for c in range(1,61): print("{:3}".format(r,c), end = " ") print()
Write the Python code that will perform this action: If your grade is 70 or better, display "You passed!" otherwise display "You failed!"
grade = eval(input("enter grade --> ")) if grade >= 70: print("You passed!") else: print("You failed!")
LCV
loop control variable
another term for LCV
loop counter
2 synonyms for Repetition
looping and iteration
Explain how a random color can be created
make the 3 integer values of red, green, and blue used to create colors random
do languages require program statments controlled by control structures to be indented
most don't, but python does
3 ways format command can format numeric output
rounding digits past decimal point, ("{:.2f}"), commas ("{:#,.2f}"), adding dollar signs ("${#,.2f}")
statement used w if for two way selection
true, statement 1
In fixed repetition, what does fixed mean
we know ahead of time how many times the certain program segment will repeat
multiway selection
when there's 2+ possible paths for a program to follow
multiple way selection
when you can choose 2+ paths
command for Conditional Repetition
while
Can control structures affect the output of a graphics program?
yes
can 1 program input many values
yes
can 3 format commands be combined into same one
yes
can keyboard input on screen affect output on graphics window
yes
is proper, consistent indentation necessary for repetition
yes
are program outputs case sensitive
yes - will not execute if letter is upper/lower case
why is using LCV better than retyping program statements over and over
you don't have to write out 20 different program statements and you don't have to count out how many times you're writing it
How many times will this program display "HELLO WORLD" ? what does it show? for p in range(11): for q in range(7): print("HELLO WORLD")
77, nested repetition
why can u not use for loop for programs requiring passwords
- "for" loop will repeat __ times no matter what pins were entered and then give you access to the program; meant to be conditional, not fixed - "while" loop will stop when correct pin is entered
control structure
- 1+ programming language statements that control the flow of a computer program - start with if
elif command
- combines "else" with the next "if" so if the first statement is false, it has another if...else statement - makes cleaner code and less indenting
nested selection
- if statement within another if statement - control structures like selection can be nested like Russian nesting dolls bc we can place one selection structure inside another
How many different colors can be created with Python
16 million
2 way selection
IF expression is true, statement1 is executed; otherwise if false (else), statement2 is executed
what must you do when you use input command
add user-friendly prompt so user knows what to type and enter into output window
programs require control structures in what languages
all
format command can change __ but not __ of numbers
appearance, value
how many program statements can be controlled by control structures
as many as you want as long as you use proper, consistent indentation
Why are 2 radint outputs not the same
bc it displays random numbers
With nested loops, the inner loop must complete all of its repetitions __________ the outer loop can simply count to the next value
before
does eval work for integers or real numbers
both
Like Selection, Repetition Control Structures have a __________ and a certain path that is only followed if it is True
condition
selection synonyms
conditional branching or decision making
repetition
creates one program segment and places that segment in some type of loop control structure that repeats 500 times
What happens if your program uses the numInput command, and the user enters something that is not a number
error message and it will force you to reenter
what does program flow follow
exact sequence of written program statements unless directed otherwise by python control instructor
simple sequence
executes program statements in exact sequence they're written
loop structure used with Fixed Repetition
for
types of input functions
input(prompt) - string input on text screen eval(input(prompt)) - number input on text screen textinput(title, prompt) - string input on graphics window numinput(title, prompt) - number input on graphics window
can you assume user knows the inner workings of your program
no
can both directions be selected in 2 way selection
no (like fork in road)
What happens if you attempt to display a number that is too big for the format to handle
numbers are displayed in their entirety but the last numbers extend beyond the format of the others
why can input function not add numbers or do math
numbers are inputted as strings, not numbers, so plus sign concatenates strings instead of adding numbers
how are numbers displayed by default
on left side of screen
one way selection
performs additional indicated action if a condition is true and simply continues regular sequence and flow if condition is false
format to display monetary values under $1000
print("${:6.2f}".format(#))
code to right justify 54 and 9876
print("{.04}".format(54)) print("{:04}".format(9876))
conditional statement
program expression that evalutates true/false
Program Flow follows the exact sequence of listed program statements, unless directed other by what
python control structure
Python has a built-in __________ library with a __________ function that generates random integers
random, radint
3 primary colors on a computer
red, green and blueWhat can give a 2-D image a 3-D appearance?
what do conditional statements require
relational operator - equals, not equals, less than, greater than, less than or equals, greater than or equals
3 types of control structures
sequence, selection, repetition
Suppose you want to use the color Metallic Gold in your program. You research and find the color numbers you need are red-212, green-175 and blue-55. Write the necessary Python command to accomplish this
setColor(212,175,55)
What can give a 2-D image a 3-D appearance
shading
Condition Repetition
situation where you don't know how many times loop will repeat; "while" loop will repeat until condition is met; ex is passwords
selection and repetition similarities
some condition can change their program flow