In this tutorial we will cover more complicated problems while covering how looping works in Python. We’ll cover for, range, floats, order of operations, while, break, continue and more. Watch the Learn to program if you missed it.
I’m covering the many different types of common problems you will see when programming and with practice you’ll vastly improve at solving any problem. Don’t worry if you get anything wrong because that is part of the learning process. The code follows the video below.
If you like videos like this consider donating a dollar on Patreon if you can.
[googleplusone]
Code & Transcript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# Every program has the ability to perform actions on a list of # values. The for loop can be used to do this. # Each time we go through the loop variable i's value will be # assigned the next value in our list for i in [2,4,6,8,10]: print("i = ", i) # We can also have range define our list for us. range(10) will # create a list starting at 0 and go up to but not include # the value passed to it. for i in range(10): print("i = ", i) # We can define the starting and ending value for range for i in range(2, 10): print("i = ", i) # You can use modulus to see if a number is odd or even # If we divide an even by 2 there will be no remainder # so if i % 2 == 0 we know it is true i = 2 print((i % 2) == 0) # ---------- PROBLEM : PRINT ODDS FROM 1 to 20 ---------- # Use a for loop, range, if and modulus to print out the odds # Use for to loop through the list from 1 to 21 for i in range(1, 21): # Use modulus to check that the result is NOT EQUAL to 0 # Print the odds if ((i % 2) != 0): print("i = ", i) # ---------- WORKING WITH FLOATS ---------- # Floating point numbers are numbers with decimal values your_float = input("Enter a float: ") # We can convert a string into a float like this your_float = float(your_float) # We can define how many decimals are printed being 2 # here by putting :.2 before f print("Rounded to 2 decimals : {:.2f}".format(your_float)) # ---------- PROBLEM : COMPOUNDING INTEREST ---------- # Have the user enter their investment amount and expected interest # Each year their investment will increase by their investment + # their investment * the interest rate # Print out their earnings after a 10 year period # Ask for money invested + the interest rate money = input("How much to invest: ") interest_rate = input("Interest Rate: ") # Convert value to a float money = float(money) # Convert value to a float and round the percentage rate by 2 digits interest_rate = float(interest_rate) * .01 # Cycle through 10 years using for and range from 0 to 9 for i in range(10): # Add the current money in the account + interest earned that year money = money + (money * interest_rate) # Output the results print("Investment after 10 years: {:.2f}".format(money)) # ---------- WORKING WITH FLOATS ---------- # When working with floats understand that they are not precise # This should print 0 but it doesn't i = 0.1 + 0.1 + 0.1 - 0.3 print(i) # Floats will print nonsense beyond 16 digits of precision i = .11111111111111111111111111111111 j = .00000000000000010000000000000001 print("Answer : {:.32}".format(i + j)) # ---------- ORDER OF OPERATIONS ---------- # When making calculations unless you use parentheses * and / # will supersede + and - print("3 + 4 * 5 = {}".format(3 + 4 * 5)) print("(3 + 4) * 5 = {}".format((3 + 4) * 5)) # ---------- THE WHILE LOOP ---------- # We can also continue looping as long as a condition is true # with a while loop # While loops are used when you don't know how many times # you will have to loop # We can use the random module to generate random numbers import random # Generate a random integer between 1 and 50 rand_num = random.randrange(1, 51) # The value we increment in the while loop is defined before the loop i = 1 # Define the condition that while true we will continue looping while (i != rand_num): # You must increment your iterator inside the while loop i += 1 # Outside of the while loop when we stop adding whitespace print("The random value is : ", rand_num) # ---------- BREAK AND CONTINUE ---------- # Continue stops executing the code that remains in the loop and # jumps back to the top # Break jumps completely out of the loop i = 1 while i <= 20: # If a number is even don't print it if (i % 2) == 0: i += 1 continue # If i equals 15 stop looping if i == 15: break # Print the odds print("Odd : ", i) # Increment i i += 1 # ---------- PROBLEM : DRAW A PINE TREE ---------- # For this problem I want you to draw a pine tree after asking the user # for the number of rows. Here is the sample program ''' How tall is the tree : 5 # ### ##### ####### ######### # ''' # You should use a while loop and 3 for loops # I know that this is the number of spaces and hashes for the tree # 4 - 1 # 3 - 3 # 2 - 5 # 1 - 7 # 0 - 9 # Spaces before stump = Spaces before top # So I need to # 1. Decrement spaces by one each time through the loop # 2. Increment the hashes by 2 each time through the loop # 3. Save spaces to the stump by calculating tree height - 1 # 4. Decrement from tree height until it equals 0 # 5. Print spaces and then hashes for each row # 6. Print stump spaces and then 1 hash # Get the number of rows for the tree tree_height = input("How tall is the tree : ") # Convert into an integer tree_height = int(tree_height) # Get the starting spaces for the top of the tree spaces = tree_height - 1 # There is one hash to start that will be incremented hashes = 1 # Save stump spaces til later stump_spaces = tree_height - 1 # Makes sure the right number of rows are printed while tree_height != 0: # Print the spaces # end="" means a newline won't be added for i in range(spaces): print(' ', end="") # Print the hashes for i in range(hashes): print('#', end="") # Newline after each row is printed print() # I know from research that spaces is decremented by 1 each time spaces -= 1 # I know from research that hashes is incremented by 2 each time hashes += 2 # Decrement tree height each time to jump out of loop tree_height -= 1 # Print the spaces before the stump and then a hash for i in range(stump_spaces): print(' ', end="") print("#") |
Leave a Reply