ENDG 233 F24 Week 3-2 Slides.pdf

Full Transcript

ENDG 233: Programming with Data Branching September 2024 Exercise – If/Elif/Else You are writing code to print the price of a bus ticket. Minors [0-5): Free fare Adults [5-65]: $3 Seniors >65: $1.2 What would the algorithm look like? Exercise – If/Elif/Else Start...

ENDG 233: Programming with Data Branching September 2024 Exercise – If/Elif/Else You are writing code to print the price of a bus ticket. Minors [0-5): Free fare Adults [5-65]: $3 Seniors >65: $1.2 What would the algorithm look like? Exercise – If/Elif/Else Start no Is 𝑎𝑔𝑒 > 65? 1. Prompt the user to enter their age Prompt user for age yes 2. Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 3. If (𝒂𝒈𝒆 < 𝟓) Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $0 4. If (𝒂𝒈𝒆 > 𝟔𝟓) no Set 𝑝𝑟𝑖𝑐𝑒 = $3 Is 𝑎𝑔𝑒 < 5? Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 Else yes Print 𝑝𝑟𝑖𝑐𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $3 Set 𝑝𝑟𝑖𝑐𝑒 = $0 5. Display (𝑝𝑟𝑖𝑐𝑒) End Exercise – If/Elif/Else Start no Is 𝑎𝑔𝑒 > 65? 1. Prompt the user to enter their age Prompt user for age yes 2. Read 𝑎𝑔𝑒 3. If (𝒂𝒈𝒆 < 𝟓) If block Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $0 4. If (𝒂𝒈𝒆 > 𝟔𝟓) no Set 𝑝𝑟𝑖𝑐𝑒 = $3 Is 𝑎𝑔𝑒 < 5? Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 Else yes Print 𝑝𝑟𝑖𝑐𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $3 Set 𝑝𝑟𝑖𝑐𝑒 = $0 5. Display (𝑝𝑟𝑖𝑐𝑒) End Exercise – If/Elif/Else Start no Is 𝑎𝑔𝑒 > 65? 1. Prompt the user to enter their age Prompt user for age yes 2. Read 𝑎𝑔𝑒 3. If (𝒂𝒈𝒆 < 𝟓) If block Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $0 4. If (𝒂𝒈𝒆 > 𝟔𝟓) If/Else block no Set 𝑝𝑟𝑖𝑐𝑒 = $3 Is 𝑎𝑔𝑒 < 5? Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 Else yes Print 𝑝𝑟𝑖𝑐𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $3 Set 𝑝𝑟𝑖𝑐𝑒 = $0 5. Display (𝑝𝑟𝑖𝑐𝑒) End Exercise – If/Elif/Else Start no Is 𝑎𝑔𝑒 > 65? 1. Prompt the user to enter their age Prompt user for age yes 2. Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 3. If (𝒂𝒈𝒆 < 𝟓) Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $0 4. If (𝒂𝒈𝒆 > 𝟔𝟓) no Set 𝑝𝑟𝑖𝑐𝑒 = $3 Is 𝑎𝑔𝑒 < 5? Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 Else yes Print 𝑝𝑟𝑖𝑐𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $3 Set 𝑝𝑟𝑖𝑐𝑒 = $0 5. Display (𝑝𝑟𝑖𝑐𝑒) End Exercise – If/Elif/Else Following the case of age = 2 Start no Is 𝑎𝑔𝑒 > 65? 1. Prompt the user to enter their age Prompt user for age yes 2. Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 3. If (𝒂𝒈𝒆 < 𝟓) Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $0 4. If (𝒂𝒈𝒆 > 𝟔𝟓) no Set 𝑝𝑟𝑖𝑐𝑒 = $3 Is 𝑎𝑔𝑒 < 5? Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 Else yes Print 𝑝𝑟𝑖𝑐𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $3 Set 𝑝𝑟𝑖𝑐𝑒 = $0 5. Display (𝑝𝑟𝑖𝑐𝑒) End age = ?, price = ? age = ?, price = ? Exercise – If/Elif/Else Following the case of age = 2 Start no Is 𝑎𝑔𝑒 > 65? 1. Prompt the user to enter their age Prompt user for age yes 2. Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 3. If (𝒂𝒈𝒆 < 𝟓) Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $0 4. If (𝒂𝒈𝒆 > 𝟔𝟓) no Set 𝑝𝑟𝑖𝑐𝑒 = $3 Is 𝑎𝑔𝑒 < 5? Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 Else yes Print 𝑝𝑟𝑖𝑐𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $3 Set 𝑝𝑟𝑖𝑐𝑒 = $0 5. Display (𝑝𝑟𝑖𝑐𝑒) End age = 2, price = ? age = 2, price = ? Exercise – If/Elif/Else Following the case of age = 2 Start no Is 𝑎𝑔𝑒 > 65? 1. Prompt the user to enter their age Prompt user for age yes 2. Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 3. If (𝒂𝒈𝒆 < 𝟓) Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $0 4. If (𝒂𝒈𝒆 > 𝟔𝟓) no Set 𝑝𝑟𝑖𝑐𝑒 = $3 Is 𝑎𝑔𝑒 < 5? Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 Else yes Print 𝑝𝑟𝑖𝑐𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $3 Set 𝑝𝑟𝑖𝑐𝑒 = $0 5. Display (𝑝𝑟𝑖𝑐𝑒) End age = 2, price = ? age = 2, price = ? Exercise – If/Elif/Else Following the case of age = 2 Start no Is 𝑎𝑔𝑒 > 65? 1. Prompt the user to enter their age Prompt user for age yes 2. Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 3. If (𝒂𝒈𝒆 < 𝟓) Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $0 4. If (𝒂𝒈𝒆 > 𝟔𝟓) no Set 𝑝𝑟𝑖𝑐𝑒 = $3 Is 𝑎𝑔𝑒 < 5? Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 Else yes Print 𝑝𝑟𝑖𝑐𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $3 Set 𝑝𝑟𝑖𝑐𝑒 = $0 5. Display (𝑝𝑟𝑖𝑐𝑒) End age = 2, price = 0 age = 2, price = 0 Exercise – If/Elif/Else Following the case of age = 2 Start no Is 𝑎𝑔𝑒 > 65? 1. Prompt the user to enter their age Prompt user for age yes 2. Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 3. If (𝒂𝒈𝒆 < 𝟓) Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $0 4. If (𝒂𝒈𝒆 > 𝟔𝟓) no Set 𝑝𝑟𝑖𝑐𝑒 = $3 Is 𝑎𝑔𝑒 < 5? Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 Else yes Print 𝑝𝑟𝑖𝑐𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $3 Set 𝑝𝑟𝑖𝑐𝑒 = $0 5. Display (𝑝𝑟𝑖𝑐𝑒) End age = 2, price = 0 age = 2, price = 0 Exercise – If/Elif/Else Following the case of age = 2 Start no Is 𝑎𝑔𝑒 > 65? 1. Prompt the user to enter their age Prompt user for age yes 2. Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 3. If (𝒂𝒈𝒆 < 𝟓) Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $0 4. If (𝒂𝒈𝒆 > 𝟔𝟓) no Set 𝑝𝑟𝑖𝑐𝑒 = $3 Is 𝑎𝑔𝑒 < 5? Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 Else yes Print 𝑝𝑟𝑖𝑐𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $3 Set 𝑝𝑟𝑖𝑐𝑒 = $0 5. Display (𝑝𝑟𝑖𝑐𝑒) End age = 2, price = 3 age = 2, price = 3 Exercise – If/Elif/Else One possible solution looks like this Start no Is 𝑎𝑔𝑒 > 65? 1. Prompt the user to enter their age Prompt user for age yes 2. Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 3. If (𝒂𝒈𝒆 < 𝟓) If block Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $0 Display (𝑝𝑟𝑖𝑐𝑒) no Set 𝑝𝑟𝑖𝑐𝑒 = $3 4. If (𝒂𝒈𝒆 > 𝟔𝟓) Is 𝑎𝑔𝑒 < 5? Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 yes Display (𝑝𝑟𝑖𝑐𝑒) If/Else block Print 𝑝𝑟𝑖𝑐𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $0 Else End Set 𝑝𝑟𝑖𝑐𝑒 = $3 Display (𝑝𝑟𝑖𝑐𝑒) Exercise – If/Elif/Else One possible solution looks like this Start no Is 𝑎𝑔𝑒 > 65? 1. Prompt the user to enter their age Prompt user for age yes 2. Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 3. If (𝒂𝒈𝒆 < 𝟓) If block Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $0 Display (𝑝𝑟𝑖𝑐𝑒) no Set 𝑝𝑟𝑖𝑐𝑒 = $3 4. If (𝒂𝒈𝒆 > 𝟔𝟓) Is 𝑎𝑔𝑒 < 5? Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 yes Display (𝑝𝑟𝑖𝑐𝑒) If/Else block Print 𝑝𝑟𝑖𝑐𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $0 Else End Set 𝑝𝑟𝑖𝑐𝑒 = $3 Display (𝑝𝑟𝑖𝑐𝑒) Exercise – If/Elif/Else The problem is that we have more than 2 possible outcomes depending on the value of a certain variable How do we skip the checking of age if the value is smaller than 5? 1. Prompt the user to enter their age 2. Read 𝑎𝑔𝑒 3. If (𝒂𝒈𝒆 ≥ 𝟓) // Check the other ages Else #age is less than 5 Set 𝑝𝑟𝑖𝑐𝑒 = $0 5. Display (𝑝𝑟𝑖𝑐𝑒) Exercise – If/Elif/Else The first concept to introduce is nesting. A nested if condition is one that appears inside another if condition block. 1. Prompt the user to enter their age Start no Is 𝑎𝑔𝑒 > 65? 2. Read 𝑎𝑔𝑒 Prompt user for age 3. If (𝒂𝒈𝒆 ≥ 𝟓) yes If (𝒂𝒈𝒆 > 𝟔𝟓) Nested If/Else Read 𝑎𝑔𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 If/Else block block Else yes Set 𝑝𝑟𝑖𝑐𝑒 = $3 Set 𝑝𝑟𝑖𝑐𝑒 = $3 Is 𝑎𝑔𝑒 >= 5? Else no Set 𝑝𝑟𝑖𝑐𝑒 = $0 Print 𝑝𝑟𝑖𝑐𝑒 Set 𝑝𝑟𝑖𝑐𝑒 = $0 5. Display (𝑝𝑟𝑖𝑐𝑒) End Exercise – If/Elif/Else The first concept to introduce is nesting. A nested if condition is one that appears inside another if condition block. main.py 1 #get the user's age 1. Prompt the user to enter their age 2 age = int(input("Please enter your age: ")) 2. Read 𝑎𝑔𝑒 3 4 #check the user's age and set the price 3. If (𝒂𝒈𝒆 ≥ 𝟓) 5 if (age >= 5): #if age >= 5 If (𝒂𝒈𝒆 > 𝟔𝟓) Nested If/Else 6 if (age > 65): #if age is larger than 65 Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 If/Else block block 7 price = 1.2 Else 8 else: #age between 5-65 9 price = 3 Set 𝑝𝑟𝑖𝑐𝑒 = $3 10 Else 11 else: # if age is less than 5 Set 𝑝𝑟𝑖𝑐𝑒 = $0 12 price = 0 13 5. Display (𝑝𝑟𝑖𝑐𝑒) 14 #output ticket price 15 print("The ticket price is $" + str(price)) Exercise – If/Elif/Else This scenario is common- so we use a new conditional block called the If/Elif/Else block. main.py 1 #get the user's age main.py 2 age = int(input("Please enter your age: ")) 1 #get the user's age 3 2 age = int(input("Please enter your age: ")) 4 #check the user's age and set the price 3 5 if (age >= 5): #if age >= 5 4 #check the user's age and set the price 6 if (age > 65): #if age is larger than 65 5 if (age < 5): #if age is less than 5 7 price = 1.2 6 price = 0 8 else: #age between 5-65 7 elif (age > 65): #if age is larger than 65 9 price = 3 8 price = 1.2 10 9 else: # else: 65 >= age >= 5 11 else: # if age is less than 5 10 price = 3 12 price = 0 11 13 12 #output ticket price 14 #output ticket price 13 print("The ticket price is $" + str(price)) 15 print("The ticket price is $" + str(price)) Exercise – If/Elif/Else You are writing a program to print the price of a bus ticket according to the following structure. Minors [0-5): Free fare Adults [5-65]: $3 Seniors >65: $1.2 The system should also check with holders of adult tickets if they are subscribed members and apply %10 discount if they are. What would the algorithm look like? Ex. Branching Inputs: age isMember (only for adult tickets) Outputs: Price of the ticket Process: 1. Check the age If age < 5 set price to 0 Else If age > 65 set price to 1.2 Else Set price to 3 If user is a member Apply %10 discount 2. Print ticket price Ex. Branching – Flowchart Start Prompt user for the age Read the age Ex. Branching – Flowchart Start Prompt user for the age Read the age no Is 𝑎𝑔𝑒 > 65? yes Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 Is 𝑎𝑔𝑒 < 5? Display 𝑝𝑟𝑖𝑐𝑒 yes Set 𝑝𝑟𝑖𝑐𝑒 = $0 End Ex. Branching – Flowchart Start Set 𝑝𝑟𝑖𝑐𝑒 = $3 Prompt user for the age Read the age no Is 𝑎𝑔𝑒 > 65? yes Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 no Is 𝑎𝑔𝑒 < 5? Display 𝑝𝑟𝑖𝑐𝑒 yes Set 𝑝𝑟𝑖𝑐𝑒 = $0 End Ex. Branching – Flowchart Start Set 𝑝𝑟𝑖𝑐𝑒 = $3 Prompt user for the age Prompt user for the membership Read the age Read the membership no Is 𝑎𝑔𝑒 > 65? yes Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 no Is 𝑎𝑔𝑒 < 5? Display 𝑝𝑟𝑖𝑐𝑒 yes Set 𝑝𝑟𝑖𝑐𝑒 = $0 End Ex. Branching – Flowchart Start Set 𝑝𝑟𝑖𝑐𝑒 = $3 Prompt user for the age Prompt user for the membership Read the age Read the membership no Is 𝑎𝑔𝑒 > 65? Is user a no member? yes yes Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 Apply %10 discount no Is 𝑎𝑔𝑒 < 5? Display 𝑝𝑟𝑖𝑐𝑒 yes Set 𝑝𝑟𝑖𝑐𝑒 = $0 End Ex. Branching – Pseudocode Set 𝑝𝑟𝑖𝑐𝑒 = 1. Prompt the user to enter the age Start $3 2. Read age Prompt user for Prompt user for 3. If (𝑎𝑔𝑒 > 65) the age the membership Set 𝑝𝑟𝑖𝑐𝑒 = $1.2 Read the Else if (𝑎𝑔𝑒 < 5) age Read the membership Set 𝑝𝑟𝑖𝑐𝑒 = $0 Else no Is 𝑎𝑔𝑒 Is user a no > 65? Set 𝑝𝑟𝑖𝑐𝑒 = $3 member? yes a) Prompt the user to check if they are a member yes Set 𝑝𝑟𝑖𝑐𝑒 = Apply %10 b) If use is member $1.2 discount 𝑝𝑟𝑖𝑐𝑒 = 𝑝𝑟𝑖𝑐𝑒 × 0.9 Is 𝑎𝑔𝑒 no 4. Display (𝑝𝑟𝑖𝑐𝑒) < 5? Display 𝑝𝑟𝑖𝑐𝑒 yes Set 𝑝𝑟𝑖𝑐𝑒 = End $0 Ex. Branching – Code main.py Set 𝑝𝑟𝑖𝑐𝑒 = Start $3 1 #get the user's age 2 age = int(input("Please enter your age: ")) Prompt user for Prompt user for 3 the age the membership 4 #check the user's age and set the price 5 if (age > 65): #if age is larger than 65 Read the Read the 6 price = 1.2 age membership 7 8 elif (age < 5): #if age is less than 5 9 price = 0 no Is 𝑎𝑔𝑒 no Is user a > 65? 10 member? 11 else: #else: 65 >= age >= 5 yes yes 12 price = 3 Set 𝑝𝑟𝑖𝑐𝑒 = Apply %10 13 isMember = input("Are you a subscribed member? (Y/N): ") $1.2 discount 14 if isMember == "Y": 15 price = price * 0.90 no Is 𝑎𝑔𝑒 Display 16 < 5? 𝑝𝑟𝑖𝑐𝑒 17 #output ticket price yes 18 print("The ticket price is $" + str(price)) Set 𝑝𝑟𝑖𝑐𝑒 = End $0 Ex. Branching – Code main.py 1 #get the user's age 2 age = int(input("Please enter your age: ")) >> python main.py 3 Please enter your age: 2 4 #check the user's age and set the price The ticket price is $0 5 if (age > 65): #if age is larger than 65 6 price = 1.2 >> python main.py 7 Please enter your age: 99 The ticket price is $1.2 8 elif (age < 5): #if age is less than 5 9 price = 0 >> python main.py 10 Please enter your age: 15 11 else: #else: 65 >= age >= 5 Are you a subscribed member? (Y/N): N 12 price = 3 The ticket price is $3 13 isMember = input("Are you a subscribed member? (Y/N): ") 14 if isMember == "Y": >> python main.py 15 price = price * 0.90 Please enter your age: 16 16 Are you a subscribed member? (Y/N): Y 17 #output ticket price The ticket price is $2.7 18 print("The ticket price is $" + str(price)) Considering Anomalies and Alternatives What happens if the user: Uses a lower case ‘y’ Uses words like ‘Yes’ Enters an age like -7 Considering Anomalies and Alternatives main.py 1 #get the user's age 2 age = int(input("Please enter your age: ")) 3 4 #check the user's age and set the price 5 if (age < 0): #if an invalid age is entered 6 print("Error! Invalid Age has been entered") 7 >> python main.py 8 else: #if a valid age is entered Please enter your age: -5 9 if (age > 65): #if age is larger than 65 Error! Invalid Age has been entered 10 price = 1.2 11 12 elif (age < 5): #if age is less than 5 >> python main.py 13 price = 0 Please enter your age: 18 14 Are you a subscribed member? (Y/N): yes 15 else: # 65 >= age >= 5 The ticket price is $2.7 16 price = 3 17 isMember = input("Are you a subscribed member? (Y/N): ") 18 if (isMember == "Y") or (isMember == "y") or (isMember == "yes") or (isMember == "Yes"): 19 price = price * 0.90 20 21 #output ticket price 22 print("The ticket price is $" + str(price)) Recap – Arithmetic Operators Arithmetic operators From Highest to lowest priority: 1. Parentheses: ( ) 2. Exponential: ** x ** y → 𝑥 𝑦 3. Multiplication: * x * y → 𝑥×𝑦 𝑥 3. Division: / x / y → 𝑦 4. Addition: + x + y 4. Subtraction: - x - y Items with the same number are evaluated left to right Integer Division and Remainder Division has 3 types Floating point division 𝑥 / 𝑦 5 / 2 → 2.5 Integer division 𝑥 // 𝑦 5 // 2 → 2 Modulo 𝑥 % 𝑦 5% 2 →1 The modulo operator return the remainder of the integer division Operation Return 0%2 0 1%2 1 2%2 0 3%2 1 Exercise Write an application that will take a number representing the total minutes from the user and output the total hours:minutes. Inputs: Total minutes Outputs: HH:MM Process: 1. Calculate the number of hours 2. Calculate the number of minutes remaining Exercise Write an application that will take a number representing the total minutes from the user and output the total hours:minutes. Start ℎ𝑜𝑢𝑟𝑠 = 𝑡𝑜𝑡𝑎𝑙_𝑚𝑖𝑛𝑢𝑡𝑒𝑠 // 60 Prompt user for the total minutes 𝑚𝑖𝑛𝑢𝑡𝑒𝑠 = 𝑡𝑜𝑡𝑎𝑙_𝑚𝑖𝑛𝑢𝑡𝑒𝑠 % 60 Display Read the total minutes hours:minutes End Exercise main.py 1 #get the total minutes 2 total_minutes = int(input("Please enter total minutes: ")) 3 Start 4 #compute hours and minutes 5 hours = total_minutes // 60 Prompt user for the 6 minutes = total_minutes % 60 total minutes 7 8 Read the total minutes 9 #output ticket price 10 print("The time in h:m is: " + str(hours) + ":" + str(minutes)) ℎ𝑜𝑢𝑟𝑠 = 𝑡𝑜𝑡𝑎𝑙_𝑚𝑖𝑛𝑢𝑡𝑒𝑠 // 60 >> python main.py >> python main.py Please enter total minutes: 61 Please enter total minutes: 60 𝑚𝑖𝑛𝑢𝑡𝑒𝑠 = 𝑡𝑜𝑡𝑎𝑙_𝑚𝑖𝑛𝑢𝑡𝑒𝑠 % 60 The time in h:m is: 1:1 The time in h:m is: 1:0 >> python main.py >> python main.py Display Please enter total minutes: 55 Please enter total minutes: 159 hours:minutes The time in h:m is: 0:55 The time in h:m is: 2:39 End 2.7.2: Compute change (modified) A cashier distributes change using the least amount of bills possible. The register contains: $5 bills $2 bills $1 bills Write a program that takes the amount of change that needs to be given and outputs the number of bills of each currency. 2.7.2: Compute change (modified) main.py 1 #input the amount of change 2 amount_to_change = int(input("enter amount of change")) 3 4 #calculate the number of bills 5 num_fives = amount_to_change // 5 6 remaining_amount = amount_to_change % 5 7 8 num_twos = remaining_amount // 2 9 num_ones = remaining_amount % 2 10 11 print('Change for $', amount_to_change) 12 print(num_fives, '$5 bill(s),', num_twos, '$2 bill(s), and', num_ones, '$1 bill(s)')

Use Quizgecko on...
Browser
Browser