180 likes | 193 Views
Implementing a program to calculate final course grades based on test and assignment averages, with different outcomes according to set criteria.
E N D
A more natural solution def highest(x,y): if x > y : return x else: return y ¿meaning? If x is bigger than y, then return the value of x, else, which means, if x is smaller or equal than y return the value of y
Problem. Toss a coin and get head or tail if random.randint(1,2) == 1: print “head” else: print “tail” ¿meaning? If the random number is 1, Then write the word “head” If not, which means the number is 2 write the word “tail”
Conditions (simple condition) • Syntax: expression1 relation-operator expression2 • relation-operator (comparing): <,>,<=,>=,==,= • Semantic: • Evaluate expressions • Compare results of expressions • if condition holds return value “True” • otherwise return value Flase • example: for an equation a*x2+bx + c = 0 • if b*b >= 4*a*c: • print “real roots” • else: • print “complex roots”
Chinga Chung function: % receives 2 integers % x y between 1-3 % 1-paper, 2- scissors 3- stone % returns: % 1 if x beats y, % 0 if none wins % -1 if y beats x
Making it shorter: Composed conditions def highest(x,y,z): if x >= y and x >= z : return x elif y >= z : return y else: return z def middle(x,y,z): if y<=x and x<=z or z<=x and x<=y: return x elif x<=y and y<=z or z<=y and y<=x: return y else: return z
Compound Condition • syntax • condition1 logic-operator condición2 . . . • logic operators: andor • unarylogicoperator: not
Problem . write a program implementing following dialogue: Mark in % ? ___ Mark in A, B, C , D, E or F = … According to the following table
Solution 1: if without else n =input(“Mark % ?”) print “Mark A,B,C,D,E,F =“, if 0 <= n and n <= 49 : print“F” if 50 <= n and n <= 59 : print“E” if 60 <= n and n <= 69 : print“D” if 70 <= n and n <= 79 : print“C” if 80 <= n and n <= 89 : print“B” if 90 <= n and n <= 100 : print“A” if n < 0 or nota > 100 : print“out of range” Note. Always evaluates 5 conditions
Solution 2: with multiple selection if 0 <= n and n <= 49 : print“F” elif 50 <= n and n <= 59 : print“E” elif 60 <= n and n <= 69 : print“D” elif 70 <= n and n <= 79 : print“C” elif 80 <= n and n <= 89 : print“B” elif 90 <= n and n <= 100 : print“A” else: print“out of range” Note. More efficient, it only evaluates until the first true condition
Solution 3: even more simple and efficient (simple conditions) if n < 0 or n > 100 : print“out of range” elif n < 50 : print“F” elif n < 60 : print“E” elif n < 70 : print“D” elif n < 80 : print“C” elif n < 90 : print“B” else: print“A”
In class exercise • Calculating the final grade of a hypothetical course reading the Test average grade (T), the Assignment average grade (A) and the grade received for participation in class (P). • The program reads the marks of 3 tests (t1, t2, t3) , two assignments (a1, a2) and the participation (p) • Then it calculates the test average (T) and assignment average (A) • Then it shows the final status of the student according to the following rules • If both averages T and A are >= 5 then the final status is calculated by considering 50% the test average, 30% the Assignments and 20% the participation and the mark is shown to the user • If the average for the tests is less than 5 then the final status should show “no pass” and the final mark is not calculated. • If the average of the tests is >= 5 but the average of the assignments is < 5 then if the participation mark is >= 8 or the average test is >= 8 then the final status is calculated as in 2. Otherwise the final status should show the word “Incomplete”