Page 104 - Programming With Python 3
P. 104

An Introduction to STEM Programming with Python — 2019-09-03a                               Page 91
            Chapter 7 — In, If, Random, and While

            overlapping values.

             Free
               1|  import random
               2|  g = random.random()
               3|  print(g)
               4|  if g >= .9:
               5|      print ("A")
               6|  if g >= .75 and g < .9:
             eBook
               7|      print("B")
               8|  if g >= .25 and g < .75:
               9|      print("C")
             10|  if g >= .10 and g < .25:
             11|      print("D")
             12|  if g < .1:
             Edition
             13|      print("F")

            Way Two: Using else statements with suites of nested if/else statements. Once a grade is found,
            the program does not execute any other of the conditions. The type of nesting may become confusing
            for very complex problems.

            Please support this work at
             14|
             15|  import random
             16|  g = random.random()
                                  http://syw2l.org
             17|  print(g)
             18|  if g >= .9:
             19|      print ("A")
             20|  else:
             21|      if g >= .75:
             22|          print("B")                                           Free
             23|      else:
             24|          if g >= .25:
             25|              print("C")
             26|          else:
             27|              if g >= .10:
             28|                  print("D")                       eBook
             29|              else:
             30|                  print("F")

            Way Three: The third solution uses the elif and else clauses to test at each grade point. When the
            first condition is found to be true, the other conditions are skipped.


             31|  import random                                 Edition
             32|  g = random.random()



            Copyright 2019 — James M. Reneau Ph.D. — http://www.syw2l.org — This work is licensed
            under a Creative Commons Attribution-ShareAlike 4.0 International License.
   99   100   101   102   103   104   105   106   107   108   109