Wk 8 Ch 4B - Loops Practice

docx

School

Lone Star College System, Woodlands *

*We aren’t endorsed by this school

Course

1315

Subject

Computer Science

Date

Feb 20, 2024

Type

docx

Pages

5

Uploaded by MateBat2484

Report
Name: _Adrian Diaz__________ Week 8 Chapter 4 Part B — Conditional Loops Conditional Loops Practice Part 1-A Practice: Shortcuts to Increment/Decrement INSTRUCTIONS: 1. Fill in any blank spaces using the other spaces to help you figure out the correct answer. 2. Given: x = 10 and y = 3 at the beginning. 3. Use the latest values as they are modified as you move from row to row. Example: In the first row x was modified from 10 to 12, while y stayed at 3. To execute the second row, use the new value of x (12) and the existing value of y (3) from row 1. Increment Standard Method Increment Shortcut Method x y 10 3 x = x + 2 x += 2 12 3 x = x * y x *= y 36 3 x = x /9 x /= 9 4 3 y = y + x y += x 4 7 y = y**2 y **=2 4 49 y = y – x y -= x 4 45 x = x**2 x **=2 16 45 Helpful Hints: Shortcut Operators for incrementing and decrementing Standard method where value = 10Shortcut methods Result value = value + 4 value += 4 value == 14 value = value - 8 value -= 8 value == 2 value = value * 5 value *= 5 value == 50 value = value / 2 value /= 2 value == 5 value = value ** 3 value **= 3 value == 1000 value = value % 7 value %= 7 value == 3 To help remember the order, the first thing you do is the math so the operator goes first followed by the assignment: *=
Name: _Adrian Diaz__________ Week 8 Chapter 4 Part B — Conditional Loops Part 2 – Using Condition Loops (Including Sentinel Values and Error Checking) Part 2-A Practice: Play Computer Using a Condition Loop and a Sentinel Value grade = int(input(“Enter a grade between 0 and 120 or enter -999 to indicate there are no more grades to enter:”) while grade != -999: if grade >= 0 and grade <= 120: total = total + grade numGrades = numGrades + 1 else: print (“Invalid grade. Please try again.”) grade = int(input(“Enter a grade between 0 and 120 or enter -999 to quit:”) avg = total / numGrades INSTRUCTIONS: Complete the table below following the program above. Loop iteration grade total numGrades 0 90 90 1 1 60 150 2 2 -1 150 2 3 70 220 3 4 -999 220 3 5 6
Name: _Adrian Diaz__________ Week 8 Chapter 4 Part B — Conditional Loops Part 2-B Practice: Writing While Condition Loop Statements Scenario Python Code As long as user wants to play computer game, continue the game. playGame = user enters Y or N While playGame is Y: play the game announce winner playGame = ask the user to enter Y or N Print “Thanks for playing!” As long as creditCard balance + new purchase is less than maximum balance, approve the purchase. cardBalance + purchase = user enters total While cardBalance + purchase < maxBalance: approve purchase show maxBalance cardBalance + purchase = user enters total Print “Thanks for purchasing!” As long as user has at least 3 gallons of gas, continue driving. gallonsGas = user enters how many gallons While gallonsGas >= 3 : continue driving gallonsGas = ask user how many gallons Print “You still can drive!” Helpful Hint: Steps to creating a conditional while statement Initialize condition variable while <condition> : <Instructions to process if condition == true> <Modify condition variable> <Regular processing instructions>
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Name: _Adrian Diaz__________ Week 8 Chapter 4 Part B — Conditional Loops Part 2-C Practice: Condition Loop for Error Checking and Counter loop for Factorial Requirements : Ask the user to enter an integer between 1 and 10. Then multiply all the digits from 1 to the number selected to display the factorial value: 4! = 1 * 2 * 3 * 4 = 24 Before starting your design, make sure you understand what steps have to be taken: · Verify that the number entered is between 1 and 10. If not, how do you display an error message and ask again? · How would you calculate the factorial? · How would you display the factorial result as shown above? Initialize Input Input and Process Output Num = int num while num < or num > 10: print “Try again” num = input result answer = 1 result = str(num) + “!=” factors = 1 for factors in range (1,num +1): answer = factors * answer result = result + str(factors) + “*” result = str result = result + “ = “ + str(answer) Insert Source Code Text (not image): #Adrian Diaz # Part-C Condition Loop for Error Checking and Counter Loop for Factorial #Initialization num = int(0) answer = int(1) factors = int(1) result = str("") #Input
Name: _Adrian Diaz__________ Week 8 Chapter 4 Part B — Conditional Loops num = int(input("\nEnter a number between 1 and 10: ")) print("")#empty space #Process & Output while num < 1 or num > 10: print("Try agian") num = int(input("\nEnter a number between 1 and 10: ")) result = str(num)+"!= " for factors in range (1,num +1): answer = factors * answer result = result + str(factors) + " * " ## This is for the above "for loop" is written in "while loop" ## while factors <= num : ## answer = factors * answer ## result = result + str(factors) + " *" ## factors += 1 print("")#empty space result = result + " = " + str(answer) print(result) Insert Output Screenshot Image: