Advertisement
tuomasvaltanen

Untitled

Oct 12th, 2022 (edited)
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.68 KB | None | 0 0
  1. # lecture 5, 12.10.2022, repetition statements
  2. print("Welcome!")
  3.  
  4. # NEW FILE
  5.  
  6. # for loop, numbers from 0 to 9
  7. # 10 - 1 = 9
  8. for x in range(10):
  9.     print(f"Cycle: {x}")
  10.  
  11. # NEW FILE
  12.  
  13. # print years from 2015 to 2022
  14. # remember, 2023 - 1 = 2022
  15. for year in range(2015, 2023):
  16.     print(year)
  17.  
  18. # NEW FILE
  19.  
  20. print("Welcome!")
  21.  
  22. # double the number on each cycle
  23. # you can try is in Python Tutor too!
  24. for cycle in range(10):
  25.     number = cycle * 2
  26.     print(number)
  27.  
  28. print("Thank you!")
  29.  
  30. # NEW FILE
  31.  
  32. # empty text variable for the loop to fill later
  33. text = ""
  34.  
  35. # for-loop, add new years to the text variable
  36. # one year at a time + one dash!
  37. for year in range(2017, 2023):
  38.     text = text + str(year) + "-"
  39.  
  40. # remove the last dash by using substrings
  41. text = text[0:-1]
  42. print(text)
  43.  
  44. # NEW FILE
  45.  
  46. print("Welcome!")
  47.  
  48. # for loop with an if-statement
  49. # even and odd numbers
  50. for x in range(10):
  51.     if x % 2 == 0:
  52.         print(x)
  53.     else:
  54.         print("Odd number...")
  55.  
  56. print("Thank you!")
  57.  
  58. # NEW FILE
  59.  
  60. counter = 1
  61.  
  62. # print the counter variable as long as it's less or equal to 10
  63. while counter <= 10:
  64.     print(counter)
  65.    
  66.     # if the counter variable is not increased by one after
  67.     # each cycle, we get an infinite loop.
  68.     # because counter will then remain at 1 forever, and
  69.     # the while condition is never met
  70.     counter = counter + 1
  71.  
  72. # NEW FILE
  73.  
  74. running = True
  75.  
  76. # while running variable == True
  77. while running:
  78.     print("Run the application!\n")
  79.  
  80.     # ask number from user, double it, and print it out
  81.     number = input("Give number:\n")
  82.     number = int(number)
  83.     total = number * 2
  84.     print(f"Doubled: {total}\n")
  85.  
  86.     # ask user if they want to continue the application
  87.     answer = input("Continue? (y/n):\n")
  88.  
  89.     # if user answers "n", quit the application
  90.     # (break out from the while-loop)
  91.     if answer == "n":
  92.         running = False
  93.  
  94. # all done, thank the user
  95. print("Thank you for using our application!")
  96.  
  97. # NEW FILE
  98.  
  99. print("Welcome!")
  100.  
  101. # first loop, 3 cycles
  102. for x in range(3):
  103.     print(f"FIRST LOOP x = {x}")
  104.  
  105.     # for EACH CYCLE in the first for-loop
  106.     # run this for-loop (5 numbers)
  107.     for y in range(5):
  108.         print(f"\tnested loop y = {y}")
  109.  
  110. # all done, thank the user
  111. print("Thank you!")
  112.  
  113. # NEW FILE
  114.  
  115. # same logic applies to for examples orders and products
  116. # each order has 5 products
  117. print("Welcome!\n")
  118.  
  119. # first loop, 3 cycles
  120. for order in range(3):
  121.     print(f"LET'S PROCESS ORDER NUMBER: {order}")
  122.  
  123.     # for EACH CYCLE in the first for-loop
  124.     # run this for-loop (5 numbers)
  125.     for product in range(5):
  126.         print(f"\tProcess product = {product} in this order")
  127.  
  128. print()
  129. # all done, thank the user
  130. print("Thank you!")
  131.  
  132. # NEW FILE
  133.  
  134. print("Let's begin!")
  135.  
  136. # five people in line waiting for their turn
  137. for person in range(5):
  138.     print(f"Person {person + 1} turn to speak!")
  139.  
  140.     # this person now says the numbers:
  141.     for number in range(4):
  142.         print(f"Person {person + 1} says number: {number + 1}!")
  143.  
  144. print()
  145. print("Everyone has spoken!")
  146.  
  147. # NEW FILE
  148.  
  149. # for loop from 0 to 199
  150. for x in range(200):
  151.    
  152.     # if x == 8, break out of the loop
  153.     # and rest of loop code and cycles are
  154.     # skipped!
  155.     if x == 8:
  156.         print("Found the data! Stop the loop.")
  157.         break
  158.  
  159.     print(x)
  160.  
  161. # all done
  162. print("All done!")
  163.  
  164. # NEW FILE
  165.  
  166. # loop, 0 to 19
  167. for x in range(20):
  168.     # if x == 8, skip the rest of the code in this
  169.     # cycle, and go to the next cycle
  170.     if x == 8:
  171.         continue
  172.  
  173.     print(x)
  174.  
  175. print("All done!")
  176.  
  177. # NEW FILE
  178.  
  179. total = 0
  180.  
  181. # ask the user a number for 100 times
  182. for x in range(100):
  183.     number = int(input("Give a number:\n"))
  184.     total = total + number
  185.  
  186. print()
  187. print(total)
  188.  
  189. # NEW FILE
  190.  
  191. cycles = int(input("How many numbers?\n"))
  192.  
  193. total = 0
  194.  
  195. # ask the user as many numbers as is set in cycles-variable!
  196. for x in range(cycles):
  197.     number = int(input("Give a number:\n"))
  198.     total = total + number
  199.  
  200. print(total)
  201.  
  202. # NEW FILE
  203.  
  204. # first numbers in Fibonacci sequence
  205. # 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
  206. # logic = each number is the sum of two previous numbers
  207. # ask the user which Fibonacci number they want to know
  208. # for example if they input 6, the number should be 5
  209. # 7 should be 8 and so on...
  210.  
  211. choice = input("Which Fibonacci number you want to know?\n")
  212. choice = int(choice) - 2
  213.  
  214. # these variables keep track of current two numbers
  215. # needed to calculate the next Fibonacci number
  216. old_number = 0
  217. new_number = 1
  218.  
  219. # the idea of this loop is to update
  220. # the next two numbers that are summed together
  221. # for the Fibonacci number
  222. # for example:
  223. # cycle 1: 0 + 1
  224. # cycle 2: 1 + 1
  225. # cycle 3: 1 + 2
  226. # cycle 2: 2 + 3
  227. # cycle 2: 3 + 5
  228. for number in range(choice):
  229.     result = old_number + new_number
  230.     old_number = new_number
  231.     new_number = result
  232.     print(result)
  233.  
  234. print(f"Fibonacci number: {result}")
  235.  
  236. # NEW FILE
  237.  
  238. # COMPOUND INTEREST WITH LOOP
  239.  
  240. # let's compute how much money we save
  241. # yearly interest 7%
  242. # 10 years time
  243. # 15000 € in the beginning
  244.  
  245. start_money = 15000
  246. interest = 1.07
  247.  
  248. # helper variable
  249. total = start_money
  250.  
  251. # yearly deposit of 2000€, added in the beginning of every year
  252. yearly_money = 2000
  253.  
  254. # compute the interest for 10 years
  255. for year in range(10):
  256.     total = total + yearly_money
  257.     total = total * interest
  258.     print(total)
  259.  
  260.  
  261. # how much money we actually earned
  262. # total - original money we invested - the 10 year investments (2000€ per year)
  263. new_money = total - start_money - (10 * yearly_money)
  264.  
  265. total = round(total, 2)
  266. new_money = round(new_money, 2)
  267.  
  268. print()
  269. print(f"Bank total in the end: {total} €")
  270. print(f"New money earned: {new_money} €")
  271.  
  272. # NEW FILE
  273.  
  274. # COMPOUND INTEREST WITH LOOP
  275.  
  276. # let's compute how much money we save
  277. # yearly interest 7%
  278. # 10 years time
  279. # 15000 € in the beginning
  280.  
  281. start_money = 15000
  282. interest = 1.07
  283.  
  284. # helper variable
  285. total = start_money
  286.  
  287. # yearly deposit of 2000€, added in the beginning of every year
  288. yearly_money = 2000
  289.  
  290. # this is our goal in saving and investments
  291. saving_target = 1000000
  292. winnings = 0
  293.  
  294. # compute the interest for 10 years
  295. for year in range(1, 31):
  296.     total = total + yearly_money
  297.     total = total * interest
  298.    
  299.     # calculate current winnings
  300.     winnings = total - start_money - (year * yearly_money)
  301.     print(winnings)
  302.  
  303.     # check our current winnings are over the target
  304.     if winnings > saving_target:
  305.         print(f"The target was met at year {year}!")
  306.         break
  307.  
  308.  
  309. # just in case we didn't manage to get to the target
  310. # inform the user
  311. if winnings < saving_target:
  312.     print("The saving target is not possible with the given years.")
  313.  
  314.  
  315.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement