Lab 2b – Home
(H2-1)
Create a program titled StudentLetterGrade
- Ask the user for the following fields:Student ID (integer)Class code (string)Final grade (float)Determine the letter grade from the final gradeA (100-93), A- (92-90), B+ (89-87), B (86-83), B- (82-80), C+ (79-77), C(76-73), C- (72-70), D+ (69-67), D (66-63), D- (62-60), F (59-0)If the number does not fall within these ranges, display an error message to the user that tells them that the final grade is out of rangeuse the Python’s built in round function to determine which letter grade is appropriatePrint the following
Student
A reminder that testing should include grades with digits after the decimal, in a couple of these letter grades, as well as entry of a grade higher than 100 and a grade lower than 0.
(H2-2)
Create a program titled DollarConversion
Ask user for the amount to be convertedif converting from dollars to euros, $1 = .92 eurosif converting from dollars to rupees, $1 = 83.16 rupees
$5.25 USD is the equivalent of 4.83 euros
$5.25 USD is the equivalent of 436.59 rupees
(H2-3)
Research Python’s shorthand if and if else syntax
Write a short program that is related to measurements of a triangle or square. It is your choice what you want the program to do. In your program, use one or more shorthand if statements and one or more shorthand if else statements.
Each line of code needs to be documented as to what it does. Describe the purpose of the method used, the parameters that the method needed, and the values you gave.
Please do your own work. Choose your own program logic. Decide how you want to implement (use) the shorthand approach to if and if/else. Identical code will receive a 0.
PYTHON
W2
Dr. Joan Lawson
PROGRAM STRUCTURE
# your name
# description of program line 1
# description of program line 2, if needed
# put user-defined functions here
def (params):
def (params):
def main ():
if __name__ == “__main__”:
main()
2
SUBMITTING HOMEWORK
•Create your python code
• your program needs to be named by the name given or a similar name
that indicates the code’s purpose
• comments must be present at the top of the code that describes the
purpose of the program
• leave whitespace – empty lines for ease of reading
• remember that indentation is critical in Python codes. Be sure to line up
your code and indent consistently – for ease of reading
•Python code
• Write all code as .py files
• Submit all .py files
•Test your code
• Create screenshots of the full execution of your programs
• It is requested that the screenshots are on a white background for
ease of reading
• No less than half the screen should be the testing environment
• Be sure to test all possible paths of the code
•Do not Zip or RAR
• attach each .py file and screenshot separately
3
4
HOMEWORK COMMENTS
The . format() method is more flexible and powerful than the %
operator, and it is often the recommended way to format
strings in Python. F-string literals are the most concise and
easy-to-read way to format strings, but they are only available
in Python 3.6 and above.
PROGRAM STRUCTURE
# your name
# description of program line 1
# description of program line 2, if needed
# put user-defined functions here
def (params):
def (params):
def main ():
if __name__ == “__main__”:
main()
5
6
PRINT FUNCTION
• formatted string literals (f-strings)
• called f-strings
• include value of Python expression inside a string by prefixing string with f or F and writing
expressions as {expressions}
• format specifier follows the variable
example below…
amount is the variable
it is being formatted as a float with commas, decimal point, and two digits after the decimal
the variable with the formatting is surrounded by curly brackets and placed in the string to print
print (f“The amount to be paid is: ${amount:,.2f}”)
AGENDA WEEK 2
Conditionals
8
BOOLEAN
• Boolean is used to evaluate the truth value of an expression.
• Relational operators
==
Equal to
Returns True if both operands are equal
!=
Not equal to
Returns True if the two operands are not equal
>
Greater than
Returns True if the left operand is greater than the right operand
<
Less than
Returns True if the left operand is less than the right operand
>=
Greater than or equal to
Returns True if the left operand is greater than or equal to the right
operand
120) and (GPA > 2.0)
or
OR
Returns True if one expression is True
if (mid_term_grade >= 3.0) or (final_grade >= 3.3)
not
NOT
if not credits > 120
Order of precedence
• NOT
• AND
• OR
Reverses the value of a Boolean expression
10
COMPARING STRINGS
• evaluated character by character from left to right
• sort sequence of digits and letters
• digits from 0-9
• uppercase letters from A-Z
• lowercase letters from a-z
• common methods used when comparing strings
• lower()
converts uppercase letters to lowercase without changing string
• upper()
converts lowercase letters to uppercase without changing string
• to use:
• variable_name.methodname()
string.lower()
11
IF STATEMENTS – BASIC
• conditionals control logic with if/else
• controls execution based on the results of a Boolean expression (True, False)
• alignment of indentation is key
• Python documentation uses the term “suite” – more common to use the term “block”
12
IF STATEMENTS – BASIC
• can be as simple as an if statement
if booleanExpression:
statements
• can be an if statement with else clause
if booleanExpression :
statements
else :
statements
13
IF STATEMENTS
(CONTINUED)
• can include one or more elif clauses. If the if statement is false, Java evaluates the first elif clause. If true, its
statements are executed, and remainder of its statements are skipped.
if booleanExpression:
statements
elif booleanExpression:
statements
else
statements
14
IF STATEMENTS – PASS
pass statement
does nothing, used because an if clause requires at least one statement
best practice:
makes if statement easily readable!
make choices that are easiest to read and understand.
avoid the use of the NOT operator…use pass statement for this
15
IF STATEMENTS – NESTED
• nested if: can code one if statement within the clause of another if statement
• be sure to use the structure that is easiest to read and understand
16
HELPING WITH CONDITIONAL LOGIC
Pseudocode – write the logic out in descriptive language, then convert to Python code
if a retail customer
and the customer is in NH, then shipping is $10
otherwise the shipping is $20
if a wholesale customer
if the customer is in NH, then shipping is $5
otherwise the shipping is $10
otherwise
shipping is $50
17
HELPING WITH CONDITIONAL LOGIC
Use a flowchart
18
LOOKING AHEAD
CONDITIONAL LOGIC – WHILE
• “condition-controlled loop”
• while statement, used to create while loops
• starts with while keyword, followed by Boolean expression, and a colon
• then an indented block of one or more statements
• statements are executed while the Boolean expression evaluates to True
• condition is tested at top of statement
• if True, statements are executed
• if False, control is returned to statement after indentation
• if the condition never evaluates to False
• infinite loop…loop does not end
19
LOOKING AHEAD
CONDITIONAL LOGIC – FOR
• “collection controlled loop”
• for statement with range
• starts with for keyword,
• name of the variable for the integer,
•
the in keyword,
•
a range() function, and a colon
• then an indented block of one or more statements
• executes the statements once for each integer in the collection of integers in the range function
• loop ends after it executes the last integer in the collection
NOW…
Work on Class Lab
Homework due by Sunday
midnight.