Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # lecture 5, 12.10.2022, repetition statements
- print("Welcome!")
- # NEW FILE
- # for loop, numbers from 0 to 9
- # 10 - 1 = 9
- for x in range(10):
- print(f"Cycle: {x}")
- # NEW FILE
- # print years from 2015 to 2022
- # remember, 2023 - 1 = 2022
- for year in range(2015, 2023):
- print(year)
- # NEW FILE
- print("Welcome!")
- # double the number on each cycle
- # you can try is in Python Tutor too!
- for cycle in range(10):
- number = cycle * 2
- print(number)
- print("Thank you!")
- # NEW FILE
- # empty text variable for the loop to fill later
- text = ""
- # for-loop, add new years to the text variable
- # one year at a time + one dash!
- for year in range(2017, 2023):
- text = text + str(year) + "-"
- # remove the last dash by using substrings
- text = text[0:-1]
- print(text)
- # NEW FILE
- print("Welcome!")
- # for loop with an if-statement
- # even and odd numbers
- for x in range(10):
- if x % 2 == 0:
- print(x)
- else:
- print("Odd number...")
- print("Thank you!")
- # NEW FILE
- counter = 1
- # print the counter variable as long as it's less or equal to 10
- while counter <= 10:
- print(counter)
- # if the counter variable is not increased by one after
- # each cycle, we get an infinite loop.
- # because counter will then remain at 1 forever, and
- # the while condition is never met
- counter = counter + 1
- # NEW FILE
- running = True
- # while running variable == True
- while running:
- print("Run the application!\n")
- # ask number from user, double it, and print it out
- number = input("Give number:\n")
- number = int(number)
- total = number * 2
- print(f"Doubled: {total}\n")
- # ask user if they want to continue the application
- answer = input("Continue? (y/n):\n")
- # if user answers "n", quit the application
- # (break out from the while-loop)
- if answer == "n":
- running = False
- # all done, thank the user
- print("Thank you for using our application!")
- # NEW FILE
- print("Welcome!")
- # first loop, 3 cycles
- for x in range(3):
- print(f"FIRST LOOP x = {x}")
- # for EACH CYCLE in the first for-loop
- # run this for-loop (5 numbers)
- for y in range(5):
- print(f"\tnested loop y = {y}")
- # all done, thank the user
- print("Thank you!")
- # NEW FILE
- # same logic applies to for examples orders and products
- # each order has 5 products
- print("Welcome!\n")
- # first loop, 3 cycles
- for order in range(3):
- print(f"LET'S PROCESS ORDER NUMBER: {order}")
- # for EACH CYCLE in the first for-loop
- # run this for-loop (5 numbers)
- for product in range(5):
- print(f"\tProcess product = {product} in this order")
- print()
- # all done, thank the user
- print("Thank you!")
- # NEW FILE
- print("Let's begin!")
- # five people in line waiting for their turn
- for person in range(5):
- print(f"Person {person + 1} turn to speak!")
- # this person now says the numbers:
- for number in range(4):
- print(f"Person {person + 1} says number: {number + 1}!")
- print()
- print("Everyone has spoken!")
- # NEW FILE
- # for loop from 0 to 199
- for x in range(200):
- # if x == 8, break out of the loop
- # and rest of loop code and cycles are
- # skipped!
- if x == 8:
- print("Found the data! Stop the loop.")
- break
- print(x)
- # all done
- print("All done!")
- # NEW FILE
- # loop, 0 to 19
- for x in range(20):
- # if x == 8, skip the rest of the code in this
- # cycle, and go to the next cycle
- if x == 8:
- continue
- print(x)
- print("All done!")
- # NEW FILE
- total = 0
- # ask the user a number for 100 times
- for x in range(100):
- number = int(input("Give a number:\n"))
- total = total + number
- print()
- print(total)
- # NEW FILE
- cycles = int(input("How many numbers?\n"))
- total = 0
- # ask the user as many numbers as is set in cycles-variable!
- for x in range(cycles):
- number = int(input("Give a number:\n"))
- total = total + number
- print(total)
- # NEW FILE
- # first numbers in Fibonacci sequence
- # 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
- # logic = each number is the sum of two previous numbers
- # ask the user which Fibonacci number they want to know
- # for example if they input 6, the number should be 5
- # 7 should be 8 and so on...
- choice = input("Which Fibonacci number you want to know?\n")
- choice = int(choice) - 2
- # these variables keep track of current two numbers
- # needed to calculate the next Fibonacci number
- old_number = 0
- new_number = 1
- # the idea of this loop is to update
- # the next two numbers that are summed together
- # for the Fibonacci number
- # for example:
- # cycle 1: 0 + 1
- # cycle 2: 1 + 1
- # cycle 3: 1 + 2
- # cycle 2: 2 + 3
- # cycle 2: 3 + 5
- for number in range(choice):
- result = old_number + new_number
- old_number = new_number
- new_number = result
- print(result)
- print(f"Fibonacci number: {result}")
- # NEW FILE
- # COMPOUND INTEREST WITH LOOP
- # let's compute how much money we save
- # yearly interest 7%
- # 10 years time
- # 15000 € in the beginning
- start_money = 15000
- interest = 1.07
- # helper variable
- total = start_money
- # yearly deposit of 2000€, added in the beginning of every year
- yearly_money = 2000
- # compute the interest for 10 years
- for year in range(10):
- total = total + yearly_money
- total = total * interest
- print(total)
- # how much money we actually earned
- # total - original money we invested - the 10 year investments (2000€ per year)
- new_money = total - start_money - (10 * yearly_money)
- total = round(total, 2)
- new_money = round(new_money, 2)
- print()
- print(f"Bank total in the end: {total} €")
- print(f"New money earned: {new_money} €")
- # NEW FILE
- # COMPOUND INTEREST WITH LOOP
- # let's compute how much money we save
- # yearly interest 7%
- # 10 years time
- # 15000 € in the beginning
- start_money = 15000
- interest = 1.07
- # helper variable
- total = start_money
- # yearly deposit of 2000€, added in the beginning of every year
- yearly_money = 2000
- # this is our goal in saving and investments
- saving_target = 1000000
- winnings = 0
- # compute the interest for 10 years
- for year in range(1, 31):
- total = total + yearly_money
- total = total * interest
- # calculate current winnings
- winnings = total - start_money - (year * yearly_money)
- print(winnings)
- # check our current winnings are over the target
- if winnings > saving_target:
- print(f"The target was met at year {year}!")
- break
- # just in case we didn't manage to get to the target
- # inform the user
- if winnings < saving_target:
- print("The saving target is not possible with the given years.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement