Commits

Yeuk Hon Wong committed 5cb5490 Draft

Added workshop3

  • Participants
  • Parent commits 329151e

Comments (0)

Files changed (2)

workshop-3/prs.py

+"""
+    Paper Rock Scissors v1
+
+    Requirements:
+        1) computer is always rock
+        2) ask user for input
+        3) tell the user the result
+
+---
+    Paper Rock Scissors v2
+    
+    Requirements:
+        1) let computer chooses (google Python random.choice, it takes a list of choices)
+        2) ask user for input
+        3) tell the user the result
+
+---
+    Paper Rock Scissors v3
+
+    Requirements:
+        Does what v2 do, except, the input is abbrv into p, r, s for paper, rock, scissors.
+    
+---
+    Paper Rock Scissors v4
+
+    Requirements:
+        1) ask user whether they want to play the game or not
+        2) does what v3 does
+        3) ask user again whether they want to continue the game or not
+
+---
+    Paper Rock Scissors v5
+
+    Requirements:
+        everything as v4 but handles bad inputs (what if user says pp instead of p?)
+
+"""
+
+
+

workshop-3/reviews.py

+# program purpose: ask user for input a, b, and c
+# calculate the quadratic formula
+# determins whether the number is real or complex root
+# if it's real, outputs the result
+# question: what if we try sqrt on negative number???
+
+user_a = int(raw_input('Enter value for a: '))   # the int(...) function converts the input from string to integer
+user_b = int(raw_input('Enter value for b: '))
+user_c = int(raw_input('Enter value for c: '))
+
+from math import sqrt  # we want the square root function
+
+"""
+    As stated in the problem, we want to determine whether the inputs
+    gives a real or complex root. We need to look at whether the number
+    b^2 - 4ac is negative or not.
+    Hence, break down the computation into parts.
+
+    1) find b^2 - 4ac (called discriminant) and determines positive or negative
+    2) if it is negative, just say it's complex because sqrt(complex_number)
+       gives error
+    3) if it is not negative, than it must be real.
+    4) if #3 is satisfied, we proceed by finding soln1 and soln2
+    
+
+    # bonus point: fact tells us if b^2 - 4ac is zero, then the whole formula
+      tells us there is one single unique solution. Can you modify the program
+      to reflect there is one real solution? Really simple with 3 lines max.
+"""
+
+discrm = (user_b ** 2) - (4 * user_a * user_c)  # you don't really need () in this case. but play safe, use it!
+
+if discrm < 0:
+    print 'You entered a = {a}, b = {b}, c = {c}, but no real solution!'.format(a=user_a, b=user_b, c=user_c)
+else: # now continue from step 3
+    soln1= (-user_b + sqrt(discrm)) / (2*user_a)
+    soln2 = (-user_b - sqrt(discrm)) / (2*user_a)
+    print 'You entered a = {a}, b = {b}, c = {c}, and the two solutions are {s1} and {s2}'.format(a=user_a, b=user_b, c=user_c, s1=soln1, s2=soln2)
+    print 'Thank you for using this program.'
+
+
+
+
+"""
+Remarks:
+
+1. the variable names user_a, user_b, user_c can be simplified as a, b, c because the whole program deals with just a b c. We know what they mean
+so it's okay to use a, b, c and such short names are descriptive enough! You can save typing :) But don't abuse this. Think about whether the variable
+name is clear enough to you and others.
+
+2. when we print we use {a} {b}, and then use format. {a} is a placeholder. the letter a inside is a placeholder name. In the dot format we match
+each placeholder name with our desired values.
+Since I want to match {a} with user_a, I just write a = user_a.
+
+There is another version of placeholder using % symbol. The eqv is:
+
+print 'You entered a = %s, b = %s, c= %s, and the two solutions are %s, and %s' % (user_a, user_b, user_c, soln1, soln2)
+
+It's ugly and hard to read. Use dot format I showed you above!
+
+3. Ask questions!
+
+"""