Loops ISYS 350 Example Fahrenheit to Celsius Use

  • Slides: 29
Download presentation
Loops ISYS 350

Loops ISYS 350

Example: Fahrenheit to Celsius Use the same formula: C=(F-32)*5/9 repeated with Fahrenheit increases with

Example: Fahrenheit to Celsius Use the same formula: C=(F-32)*5/9 repeated with Fahrenheit increases with an interval of 10 Fahrenheit 0 10 20 30 40 50 60 70 80 90 100 110 120 Celsius -17. 78 -12. 22 -6. 67 -1. 11 4. 44 10. 00 15. 56 21. 11 26. 67 32. 22 37. 78 43. 33 48. 89

Example Same formula repeated for year values with interval of 5 Future Value Table

Example Same formula repeated for year values with interval of 5 Future Value Table Present Value Rate Year 5 10 15 20 25 30 $10, 000 5% Future Value $12, 762. 82 $16, 288. 95 $20, 789. 28 $26, 532. 98 $33, 863. 55 $43, 219. 42

Straight Line Depreciation Table Straight Line Depreciation Property value Property life Year 1 2

Straight Line Depreciation Table Straight Line Depreciation Property value Property life Year 1 2 3 4 5 Value at Begin Yr 2000 1600 1200 800 400 2000 5 Amt Deprec During Yr 400 400 400 Total Deprec to End of Yr 400 800 1200 1600 2000

The while Loop • The while loop causes a statement or set of statements

The while Loop • The while loop causes a statement or set of statements to repeat as long as a Boolean expression is true • The simple logic is: While a Boolean expression is true, do some task • A while loop has two parts: – A Boolean expression that is tested for a true or false value – A statement or set of statements that is repeated a long as the Boolean expression is true Boolean Expression False True Statement(s)

Structure of a while Loop • In Python, the generic format of a while

Structure of a while Loop • In Python, the generic format of a while loop is: while Boolean. Expression: Statements • The first line is called the while clause • Statements inside the curly braces are the body of the loop • When a while loop executes, the Boolean expression is tested. If true, the statements are executed • Each time the loop executes its statement or statements, we say the loop is iterating, or performing an iteration • At the end of an iteration, it will go back to check the condition again.

Example: An Infinite Loop while 1 > 0: print("Looping")

Example: An Infinite Loop while 1 > 0: print("Looping")

Basic loop control • 1. Use a flag • 2. Use a counter •

Basic loop control • 1. Use a flag • 2. Use a counter • 3. Keep looping until reaching a stop condition

Using a flag( a Boolean variable) continue. Flag = True while continue. Flag: print("Looping!")

Using a flag( a Boolean variable) continue. Flag = True while continue. Flag: print("Looping!") if input("Continue? (Y/N): "). upper()=="N": continue. Flag=False

Example: Find the average of N numbers counter=sum=0 continue. Flag=True while continue. Flag: num=float(input("Enter

Example: Find the average of N numbers counter=sum=0 continue. Flag=True while continue. Flag: num=float(input("Enter a number: ")) counter+=1 sum+=num if input("Enter another number? (Y/N): "). upper()=="N": continue. Flag=False avg=sum/counter #print("The average of " + str(counter) +" numbers is: " + str(avg)) print(‘The average of {} numbers is: {: . 2 f}’. format(counter, avg))

MPG Example continue. Flag=True while continue. Flag: try: miles = float(input("Enter miles: ")) gallons

MPG Example continue. Flag=True while continue. Flag: try: miles = float(input("Enter miles: ")) gallons = float(input("Enter gallons used: ")) if (miles>2000): print("Miles cannot be more than 2000!") else: mpg = miles / gallons print("Miles per gallon is: " + "{: . 2 f}". format(mpg)) if input("Enter another number? (Y/N): "). upper()=="N": continue. Flag=False except Exception as ex: print(ex) Note: If 0 is entered for gallons, program catches the error and continue to run.

Using a Counter to Control the Loop counter = 1 while counter<=5: print(str(counter)) counter+=1

Using a Counter to Control the Loop counter = 1 while counter<=5: print(str(counter)) counter+=1 print("Loop stopped, and the value of counter is: " +str(counter)) # Counter initialized to 0. counter = 0 while counter<5: print(str(counter)) counter+=1 print("Loop stopped, and the value of counter is: " +str(counter))

Accumulator Find the sum of all numbers between 1 and N. sum=0 counter =

Accumulator Find the sum of all numbers between 1 and N. sum=0 counter = 1 N = int(input("Enter value N: ")) while counter <= N: sum += counter+=1 print("The sum of 1 to "+str(N)+ " is: " + str(sum))

While Loop Example N Factorial, N! NFact=counter=1 N = int(input("Enter value N: ")) while

While Loop Example N Factorial, N! NFact=counter=1 N = int(input("Enter value N: ")) while counter <= N: NFact *= counter+=1 print("The factorial of 1 to "+str(N)+ " is: " + str(NFact))

Tracing a program’s execution

Tracing a program’s execution

Loop Control Variable: starting value, step value and stop value • Fahreheit to Celsius

Loop Control Variable: starting value, step value and stop value • Fahreheit to Celsius from 0 to 120 with a step value of 10 • Print() : print a new line • Print a Tab key: t – print("t. Fahrenheit t Celsius")

Code Example start. Value=0 step. Value=10 stop. Value=120 Fahrenheit=start. Value print() print("t. Fahrenheit to

Code Example start. Value=0 step. Value=10 stop. Value=120 Fahrenheit=start. Value print() print("t. Fahrenheit to Celsius Table") print("t. Fahrenheit t Celsius") print("t_____ t _______") while Fahrenheit<=stop. Value: Celsius=(Fahrenheit-32)*5/9 print("t" + str(Fahrenheit)+ "tt " + "{: . 2 f}". format(Celsius)) Fahrenheit+=step. Value print()

Future Value Table Year’s starting value: 5, step value: 5 and stop value: 30

Future Value Table Year’s starting value: 5, step value: 5 and stop value: 30

Future Value Table Code start. Value=5 step. Value=5 stop. Value=30 year=start. Value present. Value=float(input("Enter

Future Value Table Code start. Value=5 step. Value=5 stop. Value=30 year=start. Value present. Value=float(input("Enter present value: ")) rate=float(input("Enter interest rate: ")) print("t. Future Value Table") print("t. Present value: t" +"${: , . 2 f}". format(present. Value)) print("t. Interest rate: t" +"{: . 2%}". format(rate)) print("t. Year tt Future Value") print("t_____ t _______") print() while year<=stop. Value: future. Value=present. Value*(1+rate)**year print("t" + str(year)+ "tt " + "${: , . 2 f}". format(future. Value)) year+=step. Value print()

Allow the start value, step value and stop value to change start. Value=float(input('Enter start

Allow the start value, step value and stop value to change start. Value=float(input('Enter start year: ')) step. Value=float(input('Enter step value: ')) stop. Value=float(input('Enter stop year: ')) year=start. Value present. Value=float(input("Enter present value: ")) rate=float(input("Enter interest rate: ")) print("t. Future Value Table") print("t. Present value: t" +"${: , . 2 f}". format(present. Value)) print("t. Interest rate: t" +"{: . 2%}". format(rate)) print("t. Year tt Future Value") print("t_____ t _______") print() while year<=stop. Value: future. Value=present. Value*(1+rate)**year print("t" + str(year)+ "tt " + "${: , . 2 f}". format(future. Value)) year+=step. Value print()

Display fv for rates from 5% to 10% with a step of 0. 5%

Display fv for rates from 5% to 10% with a step of 0. 5% start. Value=0. 05 step. Value=0. 005 stop. Value=0. 1 rate=start. Value present. Value=float(input("Enter present value: ")) year=float(input("Enter year: ")) print("t. Future Value Table") print("t. Present value: t" +"${: , . 2 f}". format(present. Value)) print("t. Year: tt" +str(year)) print("t. Rate tt Future Value") print("t_____ t _______") print() while rate<stop. Value+step. Value: future. Value=present. Value*(1+rate)**year print("t" + "{: . 2%}". format(rate)+ "tt " + "${: , . 2 f}". format(future. Value)) rate+=step. Value print() Note: A double may not exactly equal to. 1 while rate <. 105:

Straight Line Depreciation Table Depreciation=Property. Value/Property. Life

Straight Line Depreciation Table Depreciation=Property. Value/Property. Life

Code Example p. Value=float(input("Enter property value: ")) p. Life=float(input("Enter property life: ")) print("t. Straight

Code Example p. Value=float(input("Enter property value: ")) p. Life=float(input("Enter property life: ")) print("t. Straight Line Depreciation Table") print("t. Property value: t" +"${: , . 2 f}". format(p. Value)) print("t. Property life: t" +str(p. Life)) print("t. Year tt Value attt. Depreciationtt. Total depreciation") print("ttt Begin Yeartt. During Yeartt. To End Year") print("t___________________________________") print() value=p. Value depreciation=p. Value/p. Life total. Dep=0 year=1 while year<=p. Life: total. Dep+=depreciation print("t" + str(year)+ "tt " + "${: , . 2 f}". format(value)+ "tt" +"${: , . 2 f}". format(depreciation) + "ttt " + "${: , . 2 f}". format(total. Dep)) value-=depreciation year+=1 print()

Looping until reaching a condition hungry = True count=0 while hungry: count+=1 print("You already

Looping until reaching a condition hungry = True count=0 while hungry: count+=1 print("You already had " + str(count) +" burgers. ") if input("Still hungry? (Y/N): "). upper()=="N": hungry=False print("You had: " + str(count) +" burgers. " )

Goal Seek: Years to Reach Targeted Future Value=Present Value*(1+Rate)Year Note: When year=0, future value=present

Goal Seek: Years to Reach Targeted Future Value=Present Value*(1+Rate)Year Note: When year=0, future value=present value present. Value=float(input("Enter present value: ")) rate=float(input("Enter interest rate: ")) target=float(input("Enter target value: ")) future. Value = present. Value Year. To. Target = 0 while future. Value < target: Year. To. Target = Year. To. Target + 1 future. Value = present. Value * (1 + rate)** Year. To. Target print("It takes " + str(Year. To. Target) + " to reach the target. ")

Method 2: Using a flag present. Value=float(input("Enter present value: ")) rate=float(input("Enter interest rate: "))

Method 2: Using a flag present. Value=float(input("Enter present value: ")) rate=float(input("Enter interest rate: ")) target=float(input("Enter target value: ")) future. Value = present. Value Year. To. Target = 0 continue. Flag=True while continue. Flag: if future. Value < target: Year. To. Target = Year. To. Target + 1 future. Value = present. Value * (1 + rate)** Year. To. Target else: continue. Flag=False print("It takes " + str(Year. To. Target) + " to reach the target. ")

Exiting a loop using the break statement • In some cases it is necessary

Exiting a loop using the break statement • In some cases it is necessary to end a loop before the test condition would end it • Use the break statement: break

Example hungry = True count=0 while hungry: count+=1 print("You already had " + str(count)

Example hungry = True count=0 while hungry: count+=1 print("You already had " + str(count) +" burgers. ") if input("Still hungry? (Y/N): "). upper()=="N": break print("You had: " + str(count) +" burgers. " )

Using break statement present. Value=float(input("Enter present value: ")) rate=float(input("Enter interest rate: ")) target=float(input("Enter target

Using break statement present. Value=float(input("Enter present value: ")) rate=float(input("Enter interest rate: ")) target=float(input("Enter target value: ")) future. Value = present. Value Year. To. Target = 0 continue. Flag=True while continue. Flag: if future. Value < target: Year. To. Target = Year. To. Target + 1 future. Value = present. Value * (1 + rate)** Year. To. Target else: break print("It takes " + str(Year. To. Target) + " to reach the target. ")