Advertisement
tuomasvaltanen

Untitled

Oct 11th, 2021 (edited)
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.51 KB | None | 0 0
  1. # Adobe Connect -lecture, 11.10.2021
  2. print("Repetition statements today!")
  3.  
  4. # basic for loop, numbers 0-9
  5. for x in range(10):
  6.     print(f"Cycle number: {x}")
  7.  
  8.  
  9. print()
  10.  
  11. # we can also have two numbers for the
  12. # for loop's range
  13. # example: years from 2016 => 2021
  14. start = 2016
  15. end = 2021
  16.  
  17. for year in range(start, end + 1):
  18.     print(year)
  19.  
  20. # NEW FILE
  21.  
  22. # initalize empty string
  23. text = ""
  24.  
  25. # build text in a loop
  26. for year in range(2016, 2021):
  27.     text = text + str(year) + "-"
  28.  
  29. # print the result
  30. print(text)
  31.  
  32. # NEW FILE
  33.  
  34. # initalize empty string
  35. text = ""
  36.  
  37. # build text in a loop, incremented by two years after reach cycle
  38. for year in range(2012, 2021, 2):
  39.     text = text + str(year) + "-"
  40.  
  41. # print the result
  42. print(text)
  43.  
  44. # NEW FILE
  45.  
  46. counter = 1
  47.  
  48. while counter <= 10:
  49.     print(counter)
  50.     counter = counter + 1
  51.  
  52. # NEW FILE
  53.  
  54. # run the application as long as the user wants to
  55.  
  56. # start the program, use this variable to determine if application is running
  57. running = True
  58.  
  59. while running:
  60.     print("Let's run the application!")
  61.  
  62.     # do the actual program
  63.     number = input("Give a number:\n")
  64.     number = int(number)
  65.  
  66.     result = number * 2
  67.     print(f"Your number multiplied by 2 equals to {result}!")
  68.  
  69.     # check if the user wants to continue or quit
  70.     choice = input("Would you like to continue? (y/n)\n")
  71.  
  72.     if choice == 'n':
  73.         running = False
  74.  
  75. print("Thank you for using our application!")
  76.  
  77. # NEW FILE
  78.  
  79. # nested loops
  80. print("Start of the application!")
  81.  
  82. for x in range(3):
  83.     print(f"FIRST LOOP: x = {x}")
  84.  
  85.     for y in range(5):
  86.         print(f"\tNested loop: y = {y}")
  87.  
  88. print("End of the application!")
  89.  
  90. # NEW FILE
  91.  
  92. # nested loops
  93.  
  94. # five persons
  95. # each person says numbers 1-4 on their turn
  96. # after all have spoken, print "Everyone has spoken!"
  97.  
  98. # go through all persons
  99. for person in range(5):
  100.     print(f"Person {person + 1} turn!")
  101.  
  102.     # say the numbers for each person
  103.     for number in range(4):
  104.         print(f"Person {person + 1} says: {number + 1}")
  105.  
  106. print("Everyone has spoken!")
  107.  
  108. # NEW FILE
  109.  
  110. # demonstrating the break command
  111. # the loop ends earlier when using break-command
  112. print("Application start!")
  113.  
  114. for x in range(20):
  115.     if x == 7:
  116.         break
  117.  
  118.     print(x)
  119.  
  120.  
  121. print("Application ends!")
  122.  
  123. # NEW FILE
  124.  
  125. # continue-command will skip the active iteration if called
  126. print("Application start!")
  127.  
  128. for x in range(20):
  129.     if x == 7:
  130.         continue
  131.  
  132.     print(x)
  133.  
  134.  
  135. print("Application ends!")
  136.  
  137. # NEW FILE
  138.  
  139. # compound interest (interset of interst)
  140.  
  141. # interest of interest (compound interest)
  142. # for example if you have starting money of 10000
  143. # and 7% yearly interest, let's use loop to calculate how much
  144. # new money we have after the saving
  145.  
  146. # we can do this with loops, without the mathematics...
  147. start_money = 15000
  148.  
  149. # new money added to savings in the beginning of each year
  150. yearly_money = 2000
  151. total = start_money
  152.  
  153. # use decimal format to INCREASE THE MONEY 7% a year
  154. interest = 1.07
  155.  
  156. # ten years!
  157. for year in range(10):
  158.     total = total + yearly_money
  159.     total = total * interest
  160.  
  161. total = round(total, 2)
  162. print(total)
  163.  
  164. new_money = total - start_money - (yearly_money * 10)
  165. print(new_money)
  166.  
  167. # NEW FILE
  168.  
  169. # interest of interest (compound interest)
  170. # for example if you have starting money of 10000
  171. # and 7% yearly interest, let's use loop to calculate how much
  172. # new money we have after the saving
  173.  
  174. # we can do this with loops, without the mathematics...
  175. start_money = 15000
  176.  
  177. # new money added to savings in the beginning of each year
  178. yearly_money = 2000
  179. total = start_money
  180.  
  181. # use decimal format to INCREASE THE MONEY 7% a year
  182. interest = 1.07
  183.  
  184. # what is our save target
  185. target_save = 50000
  186.  
  187. # keep track of our winnings so far
  188. winnings = 0
  189.  
  190. # twenty years!
  191. for year in range(20):
  192.     total = total + yearly_money
  193.     total = total * interest
  194.  
  195.     # the winnings so far in this CYCLE
  196.     winnings = total - start_money - ((year + 1) * yearly_money)
  197.  
  198.     # check if our winnings are already enough
  199.     if winnings >= target_save:
  200.         print("YES! MONEY TARGET ACHIEVED!")
  201.         print(f"It took {year + 1} years!")
  202.         break
  203.  
  204.  
  205. # check if we didn't match our target
  206. if winnings < target_save:
  207.     print("It's not possible to get this saving with these parameters.")
  208.     print("Start money, amount of years, interest rate, target save etc.")
  209.  
  210. total = round(total, 2)
  211. print(total)
  212.  
  213. new_money = total - start_money - (yearly_money * 10)
  214. print(new_money)
  215.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement