Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Adobe Connect -lecture, 11.10.2021
- print("Repetition statements today!")
- # basic for loop, numbers 0-9
- for x in range(10):
- print(f"Cycle number: {x}")
- print()
- # we can also have two numbers for the
- # for loop's range
- # example: years from 2016 => 2021
- start = 2016
- end = 2021
- for year in range(start, end + 1):
- print(year)
- # NEW FILE
- # initalize empty string
- text = ""
- # build text in a loop
- for year in range(2016, 2021):
- text = text + str(year) + "-"
- # print the result
- print(text)
- # NEW FILE
- # initalize empty string
- text = ""
- # build text in a loop, incremented by two years after reach cycle
- for year in range(2012, 2021, 2):
- text = text + str(year) + "-"
- # print the result
- print(text)
- # NEW FILE
- counter = 1
- while counter <= 10:
- print(counter)
- counter = counter + 1
- # NEW FILE
- # run the application as long as the user wants to
- # start the program, use this variable to determine if application is running
- running = True
- while running:
- print("Let's run the application!")
- # do the actual program
- number = input("Give a number:\n")
- number = int(number)
- result = number * 2
- print(f"Your number multiplied by 2 equals to {result}!")
- # check if the user wants to continue or quit
- choice = input("Would you like to continue? (y/n)\n")
- if choice == 'n':
- running = False
- print("Thank you for using our application!")
- # NEW FILE
- # nested loops
- print("Start of the application!")
- for x in range(3):
- print(f"FIRST LOOP: x = {x}")
- for y in range(5):
- print(f"\tNested loop: y = {y}")
- print("End of the application!")
- # NEW FILE
- # nested loops
- # five persons
- # each person says numbers 1-4 on their turn
- # after all have spoken, print "Everyone has spoken!"
- # go through all persons
- for person in range(5):
- print(f"Person {person + 1} turn!")
- # say the numbers for each person
- for number in range(4):
- print(f"Person {person + 1} says: {number + 1}")
- print("Everyone has spoken!")
- # NEW FILE
- # demonstrating the break command
- # the loop ends earlier when using break-command
- print("Application start!")
- for x in range(20):
- if x == 7:
- break
- print(x)
- print("Application ends!")
- # NEW FILE
- # continue-command will skip the active iteration if called
- print("Application start!")
- for x in range(20):
- if x == 7:
- continue
- print(x)
- print("Application ends!")
- # NEW FILE
- # compound interest (interset of interst)
- # interest of interest (compound interest)
- # for example if you have starting money of 10000
- # and 7% yearly interest, let's use loop to calculate how much
- # new money we have after the saving
- # we can do this with loops, without the mathematics...
- start_money = 15000
- # new money added to savings in the beginning of each year
- yearly_money = 2000
- total = start_money
- # use decimal format to INCREASE THE MONEY 7% a year
- interest = 1.07
- # ten years!
- for year in range(10):
- total = total + yearly_money
- total = total * interest
- total = round(total, 2)
- print(total)
- new_money = total - start_money - (yearly_money * 10)
- print(new_money)
- # NEW FILE
- # interest of interest (compound interest)
- # for example if you have starting money of 10000
- # and 7% yearly interest, let's use loop to calculate how much
- # new money we have after the saving
- # we can do this with loops, without the mathematics...
- start_money = 15000
- # new money added to savings in the beginning of each year
- yearly_money = 2000
- total = start_money
- # use decimal format to INCREASE THE MONEY 7% a year
- interest = 1.07
- # what is our save target
- target_save = 50000
- # keep track of our winnings so far
- winnings = 0
- # twenty years!
- for year in range(20):
- total = total + yearly_money
- total = total * interest
- # the winnings so far in this CYCLE
- winnings = total - start_money - ((year + 1) * yearly_money)
- # check if our winnings are already enough
- if winnings >= target_save:
- print("YES! MONEY TARGET ACHIEVED!")
- print(f"It took {year + 1} years!")
- break
- # check if we didn't match our target
- if winnings < target_save:
- print("It's not possible to get this saving with these parameters.")
- print("Start money, amount of years, interest rate, target save etc.")
- total = round(total, 2)
- print(total)
- new_money = total - start_money - (yearly_money * 10)
- print(new_money)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement