Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class 14 functions in Python can return multiple values counting loops

Similar presentations


Presentation on theme: "Class 14 functions in Python can return multiple values counting loops"— Presentation transcript:

1 Class 14 functions in Python can return multiple values counting loops
while loops interactive loops sentinel loops

2 functions in Python can return multiple values
(NOT true in many languages)

3 functions in Python can return multiple values
def dater(): dateAsStr = input("Enter date: mm/dd/yyyy > ") dateAsList = dateAsStr.split("/") m=int(dateAsList[0]) d=int(dateAsList[1]) y=int(dateAsList[2]) return d, m, y

4 functions in Python can return multiple values
def dater(): dateAsStr = input("Enter date: mm/dd/yyyy > ") dateAsList = dateAsStr.split("/") m=int(dateAsList[0]) d=int(dateAsList[1]) y=int(dateAsList[2]) return d, m, y >>> day, month, year = dater()

5 functions in Python can return multiple values
>>> day, month, year = dater() Enter date: mm/dd/yyyy > 10/24/2018 >>> month 10 >>> day 24 >>> year 2018

6 Scenario 1: input the number of employees
Problem: Compute the payroll for a company. input: hours and payrate for each worker output: total amount paid by company

7 Scenario 1: input the number of employees
Problem: Compute the payroll for a company. input: hours and payrate for each worker output: total amount paid by company Know the number of times to go through the loop accumulate (total amount paid)

8 Know the number of times to go through the loop
accumulate (total amount paid) input the total count of data, n initialize total loop n times input a value, x add x to total output total

9 x=float(input("Enter a number>>")) total=total + x
Know the number of times to go through the loop accumulate (total amount paid) input the total count of data, n initialize total loop n times input a value, x add x to total output total for i in range(n): x=float(input("Enter a number>>")) total=total + x

10 while loop

11 while loop while <condition>: <body>

12 We can imitate a for i in range loop with a while loop
for i in range(n): x=float(input("Enter a number>>")) total=total + x i=0 while i < n: x=float(input("Enter a number>> ")) total=total + x i=i+1

13 Scenario 2: keep asking if there are more employees
Problem: Compute the payroll for a company. input: hours and payrate for each worker output: total amount paid by company DON'T KNOW the number of times to go through the loop accumulate (total amount paid)

14 DON'T KNOW the number of times to go through the loop
accumulate (total amount paid) "Interactive Loop" initialize total set moredata to "yes" while moredata is "yes" input a value, x add x to total ask user if there is moredata output total

15 DON'T KNOW the number of times to go through the loop
accumulate (total amount paid) "Interactive Loop" total = 0 moredata = "yes" while moredata== "yes": x=float(input("Enter a number>> ")) total = total + x moredata=input("Do you have more data? (yes or no)") print(total)

16 Scenario 3: Too many employees. Use a sentinel
Problem: Compute the payroll for a company. input: hours and payrate for each worker output: total amount paid by company DON'T KNOW the number of times to go through the loop accumulate (total amount paid)

17 General sentinel loop get the first data item while item is not the sentinel process the item get the next data item

18 DON'T KNOW the number of times to go through the loop
accumulate (total amount paid) "Sentinel Loop" initialize total get first value, x while x is not the sentinel add x to total input a value, x output total

19 DON'T KNOW the number of times to go through the loop
accumulate (total amount paid) "Sentinel Loop" total = 0 x = float(input("Enter a number, negative to quit>>")) while x>=0: total = total + x print(total)

20


Download ppt "Class 14 functions in Python can return multiple values counting loops"

Similar presentations


Ads by Google