🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Practical 3_3_Algorithm and Control Statement_3_v1.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Official (Open) Practical 3_3_Algorithm and Control Statement_3_v1.docx Topic 3 Loops and Iteration...

Official (Open) Practical 3_3_Algorithm and Control Statement_3_v1.docx Topic 3 Loops and Iteration Objectives On successful completion of this practical, the students should be able to: apply count–controlled repetition structure for repeated code execution in a program apply condition – controlled repetition structure for repeated code execution in a program Question 1 a) Write a Python for loop using the range() function to print out your name 100 times. This could simulate a scenario where you need to send repeated notifications or updates in a batch process. How would you implement this? b) Convert the above for loop to a while loop. Question 2 a) Write a Python script to simulate sending notifications every other day during a 10-day period. Use a range() function with a step increment to ensure notifications are sent only on odd-numbered days (i.e., Days 1, 3, 5, 7, and 9). Each time a notification is sent, the script should print a message stating that a notification has been sent on that day. After completing the sequence, the script should output a final message confirming that all scheduled notifications have been issued. Sample Output Notification sent on Day 1 Notification sent on Day 3 Notification sent on Day 5 Notification sent on Day 7 Notification sent on Day 9 All scheduled notifications have been sent! b) Convert the above for loop to a while loop Question 3 a) Write a Python script to simulate a countdown timer for the launch of a new product. The script should start counting down from 10 to 1, with each number displayed on the same line followed by a space. Once the countdown reaches 0, the script should print 'Blast off!' to signify the launch. This countdown could represent the final seconds before a product becomes available on a website or before a major event begins. How would you implement this in Python? [Hint: add the option end=’’ into the print function] Sample Output 10 9 8 7 6 5 4 3 2 1 Blast off! b) Convert the above while loop to a for loop using the range(begin, end, step) function Page 1 Official (Open) Practical 3_3_Algorithm and Control Statement_3_v1.docx Question 4 a) Write a Python program that prints out a list of subjects, numbering them in ascending order. The program should use a left-aligned format for the numbering, ensuring that the 'No' column is 5 characters wide. The subject names should follow immediately after the number in a neatly aligned manner. Purpose of the Alignment: The alignment and spacing in the output are crucial for creating a clear and readable display, particularly when this list is intended to be part of a larger report, a content management system where data clarity and ease of reading are important. Proper formatting ensures that the list is easy to scan through, making it more user-friendly for both digital displays and printed materials. It also aids in maintaining consistency in visual presentation, which is vital in professional documentation. [Hint: use the F-format method to print out the table formatting] No Subject --------------- 1 English 2 Math 3 Science 4 History b) Rewrite the program using the while loop. [Hint: use len() function and the F-string method to print out the table formatting. Take this opportunity to google. You need to learn how to look for answers.] Question 5 Write a Python program that prompts the user to enter a non-negative integer and then calculates the factorial of that number using a loop. The factorial function, denoted by an exclamation point (!), is the product of all whole numbers from the chosen number down to 1. Additional Resources: For a deeper understanding of the factorial function, you can watch this explanatory video on YouTube: What is factorial https://www.youtube.com/watch?v=eXcJOgNPYJE For the input 8, the program should compute and display: example: 8! = 8 𝑥𝑥 7 𝑥𝑥 6𝑥𝑥 5 𝑥𝑥 4 𝑥𝑥 3 𝑥𝑥 2 𝑥𝑥 1 Sample Output Enter a non-negative number: 8 Factorial of 8 is 40320 Page 2 Official (Open) Practical 3_3_Algorithm and Control Statement_3_v1.docx Question 6 (GROUP) Write a program that calculates the average test scores of each student. a) Prompt the user to enter the number of students and the number of tests per subject. b) Loop through each student and ask for their scores on each test. c) Calculate and display the average score for each student to two decimal places. This script is useful for teachers who need to calculate and keep track of students' average scores for a specific module. It automates the average calculation and ensures the results are presented in a clear and precise manner. Sample Output ** By working on this programming task, students not only learn how to write Python code but also how to apply programming skills to solve practical problems and present data effectively. This builds a foundation for more advanced programming and data handling tasks they may encounter in their careers or further studies. Page 3

Use Quizgecko on...
Browser
Browser