|
| 1 | + |
| 2 | +<title>Animal Years</title> |
| 3 | + |
| 4 | +<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 5 | + |
| 6 | +<meta http-equiv="content-type" content="text/html; charset=UTF8"> |
| 7 | + |
| 8 | + |
| 9 | +<!-- Bootstrap --> |
| 10 | +<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> |
| 11 | +<link rel="stylesheet" type="text/css" href="../../style.css"> |
| 12 | + |
| 13 | +<!-- Java Script --> |
| 14 | +<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> |
| 15 | +<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> |
| 16 | +<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +<!-- font awesome --> |
| 21 | + <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous"> |
| 22 | + |
| 23 | +<!-- SWAL --> |
| 24 | +<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script> |
| 25 | + |
| 26 | + |
| 27 | +<!-- Stanford --> |
| 28 | +<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700' rel='stylesheet' type='text/css'> |
| 29 | +<link href='https://fonts.googleapis.com/css?family=Source+Serif+Pro:400,600,700' rel='stylesheet' type='text/css'> |
| 30 | + |
| 31 | + |
| 32 | +<body> |
| 33 | + |
| 34 | + <div class="container container-course"> |
| 35 | + <div class="row"> |
| 36 | + <div class="col"> |
| 37 | + <h1>Animal Years</h1> |
| 38 | + <p class="subtleHeading">Problem written by Lucia</p> |
| 39 | + <hr/> |
| 40 | + <p> |
| 41 | + Other animals typically have different lifespans than humans. If you divide the average human lifespan by the average lifespan of another animal, you can calculate the multiplier for converting an age in human years to an age in animal years. For example, the average lifespan of a human is 79 years and the average lifespan of a dog is 11 years. So, 1 human year is equal to 79/11 = 7.18 dog years. Then, to convert say 3 human years to dog years, you'd multiply 3 * 7.18 = 21.54 dog years. That means, if your dog is 3 years old in human years, they're past their teenage years in dog years! |
| 42 | +</p> |
| 43 | + |
| 44 | +<p> Write a program that converts human years to animal years (of any animals of your choosing!):</p> |
| 45 | +<p></p> |
| 46 | +<center> |
| 47 | + <img style="width:800px" src="./demo1.png"> |
| 48 | +</center> |
| 49 | +<p></p> |
| 50 | +<center> |
| 51 | + <img style="width:800px" src="./demo2.png"> |
| 52 | +</center> |
| 53 | +<p></p> |
| 54 | + |
| 55 | +<p> |
| 56 | + Since the multipliers for each animal will never change, you should store these as constants. |
| 57 | +</p> |
| 58 | + <h2>Solution</h2> |
| 59 | + <p> |
| 60 | + <a class="btn btn-primary" id="soln-btn" onclick="toggleButtonText()" |
| 61 | + data-toggle="collapse" href="#soln-collapse" aria-expanded="false" |
| 62 | + aria-controls="Collapse"> |
| 63 | + Show Solution |
| 64 | + </a> |
| 65 | + </p> |
| 66 | + <div class="collapse" id="soln-collapse"> |
| 67 | + <pre class="console" id="editor" style="height:940.0px"># constants |
| 68 | +CAT_YRS_MULTIPLIER = 4.9 |
| 69 | +DOG_YRS_MULTIPLIER = 7.18 |
| 70 | +ELEPHANT_YRS_MULTIPLIER = 1.13 |
| 71 | +LION_YRS_MULTIPLIER = 5.64 |
| 72 | +MONKEY_YRS_MULTIPLIER = 3.95 |
| 73 | +RABBIT_YRS_MULTIPLIER = 7.9 |
| 74 | +HAMSTER_YRS_MULTIPLIER = 39.5 |
| 75 | + |
| 76 | +def main(): |
| 77 | + print('This program converts human years to animal years.') |
| 78 | + |
| 79 | + # gets two user inputs, the human years to convert and the animal type |
| 80 | + human_yrs_str = input('Human years: ') |
| 81 | + animal_type_str = input('Animal type (options: cat, dog, elephant, lion, monkey, rabbit, hamster): ') |
| 82 | + |
| 83 | + # converts human years to animal years of the animal type specified |
| 84 | + animal_yrs = None |
| 85 | + |
| 86 | + # casts human_yrs_str, a string type variable, to a float type variable |
| 87 | + human_yrs = float(human_yrs_str) |
| 88 | + |
| 89 | + # converts human years to animal years based on the animal type input |
| 90 | + if animal_type_str == 'cat': |
| 91 | + animal_yrs = human_yrs * CAT_YRS_MULTIPLIER |
| 92 | + elif animal_type_str == 'dog': |
| 93 | + animal_yrs= human_yrs * DOG_YRS_MULTIPLIER |
| 94 | + elif animal_type_str == 'elephant': |
| 95 | + animal_yrs = human_yrs * ELEPHANT_YRS_MULTIPLIER |
| 96 | + elif animal_type_str == 'lion': |
| 97 | + animal_yrs = human_yrs * LION_YRS_MULTIPLIER |
| 98 | + elif animal_type_str == 'monkey': |
| 99 | + animal_yrs = human_yrs * MONKEY_YRS_MULTIPLIER |
| 100 | + elif animal_type_str == 'rabbit': |
| 101 | + animal_yrs = human_yrs * RABBIT_YRS_MULTIPLIER |
| 102 | + elif animal_type_str == 'hamster': |
| 103 | + animal_yrs = human_yrs * HAMSTER_YRS_MULTIPLIER |
| 104 | + |
| 105 | + # prints the equivalent animal years for the human year input |
| 106 | + # prints error message in animal type was invalid |
| 107 | + if animal_yrs: |
| 108 | + print(human_yrs_str + ' human years is equal to ' + str(animal_yrs) + ' ' + animal_type_str + ' years.') |
| 109 | + else: |
| 110 | + print('Animal type is invalid.') |
| 111 | + |
| 112 | +if __name__ == '__main__': |
| 113 | + main()</pre> |
| 114 | + </div> |
| 115 | + |
| 116 | + <script src="../../plugins/ace/ace.js" type="text/javascript" charset="utf-8"></script> |
| 117 | + <script> |
| 118 | + var editor = ace.edit("editor"); |
| 119 | + editor.setTheme('ace/theme/eclipse'); |
| 120 | + editor.getSession().setMode("ace/mode/python"); |
| 121 | + editor.setReadOnly(true); |
| 122 | + editor.renderer.setShowGutter(false); |
| 123 | + editor.setFontSize("14px"); |
| 124 | + /*editor.setTheme("ace/theme/eclipse"); |
| 125 | + editor.getSession().setMode("ace/mode/java");*/ |
| 126 | + </script> |
| 127 | + <script> |
| 128 | + function toggleButtonText() { |
| 129 | + var elem = document.getElementById("soln-btn"); |
| 130 | + if (elem.innerHTML.trim() === "Show Solution") { |
| 131 | + elem.innerHTML = "Hide Solution"; |
| 132 | + } else { |
| 133 | + elem.innerHTML = "Show Solution"; |
| 134 | + } |
| 135 | + } |
| 136 | + </script> |
| 137 | + <hr/> |
| 138 | + </div> |
| 139 | + </div> |
| 140 | + </div> |
| 141 | + |
| 142 | +</body> |
0 commit comments