top of page

# Search CSNewbs

227 items found for ""

• Computer Science Newbies

C omputer S cience P ros Official 2023 CSPros: Diyar Henry Rahul Hussain Alex B Sam Will Zain Iman Aidan Tomos Harry Alex H Alex I-C

• Python | Section 3 Practice Tasks | CSNewbs

top Python - Section 3 Practice Tasks Task One: Square Number Create a program that asks the user to input a number and then prints the square of that number - to do this, multiply the number by itself . ​ Requirements for a complete program: Use only two lines. Include the user's number and the squared number in the print line. Remember: Break up variables and parts of a sentence in a print line by using commas. Example solutions: Enter a number: 12 The square of 12 is 144 Enter a number: 7 The square of 7 is 49 Task Two: Multiplying Numbers X Create a program that asks the user to input two numbers. Multiply the two numbers together and print the total. Requirements for a complete program: Only three lines. Use only one print line. Include the user's number and the squared number in the print line. Remember: Break up variables in a print line by using commas or plus signs between each part of the "sentence" . Example solution: Task Three: Turning 65 Create a program that asks the user to input their current age and then prints a guess of when they will turn 65. (Note this is more likely to be correct towards the end of the year – think about why). ​ You could do this in just two lines but before trying that work out on paper the steps to calculating your own age you will turn 65. What steps did you take? Try to recreate those steps in Python. You might need to create another variable to make it easier. Example solution: Task Four: Multiplication Table Let the user enter a number then print the first five multiplications in its times table. Simple example solution: Better example solution: ⬅ 3b - Simple Calculations 4a - If Statements ➡

• Python | 4b - Mathematical Operators | CSNewbs

• Python | Section 2 Practice Tasks | CSNewbs

• Computer Science Newbies

C omputer S cience Newb ie s Popular CSNewbs topics: Programming PYTHON GCSE Computer Science OCR GCSE Computer Science EDUQAS OCR Cambridge Technicals Level 3 IT You are viewing the mobile version of CSNewbs. The site may appear better on a desktop or laptop . Programming HTML CSNewbs last updated: Wednesday 24th May 2023 Over 342,000 visits in the past year! About CSNewbs

• Python | CSNewbs

• Python | 3b - Simple Calculations | CSNewbs

• Python | 2a - Inputting Text | CSNewbs

• Python | 12 - Error Handling | CSNewbs

Python 12 - Error Handling Errors When an error occurs in Python, you may see a chunk of red text like this. ​ This is very useful when creating programs as it tells us the exact line of the error (10), and its type (NameError). ​ However, a completed program should have code in place for when an unexpected error occurs – we call this exception handling . General Exception In this example, Python will attempt to run the code indented beneath try . If there are no errors then the code will stop just before except . If an error does occur then the Exception code will be run . If we enter a correct value then the program will execute normally: But if an error occurs (such as writing a string when an integer is expected) then the Exception code will run : You can add the else command to your code that will execute only if there are no errors : If a valid number is entered then the else code will be printed: If a code generating an error is entered then the except code will be printed: Practice Task 1 Create a program that asks the user to input their age. ​ Don't forget to use the int command. ​ Use try and except to print a message if a number is not inputted. Example solution: Specific Exceptions The Exception command used in the section above is for any general error that occurs. You can also use specific except commands for a variety of errors. ​ Below is a program with two different specific exception commands for one try statement: If a Value Error occurs, such as when the wrong data type is entered , then related code will be printed: Or if the user tries to divide by zero then a Zero Division Error will be triggered which prints a relevant response: Other types of exception can be found here . Practice Task 2 Create a program that asks the user to input a number and then divides this value by 999. ​ Create a Value Error and Zero Division Error exception and include an appropriate message in both. Example solution for Zero Division: ⬅ 11 - Graphical User Interfac e Extended Task 1 (Pork Pies) ➡

• Python | 8c - Dictionaries | CSNewbs