type
status
slug
summary
tags
category
icon
password
✰ღ★ღ Squaring an Argument ღ★ღ✰
Write a function that takes an argument and returns the square of it.
★ Problem Breakdown ★
- The function needs to take an argument and return the square.
- The square of a number is the result of multiplying that number by itself. For example:
★ Flowchart ★

★ Code ★
- We take the argument and multiply it by itself:
✰ღ★ღ Even or Odd ღ★ღ✰
Create a function that takes an integer as an argument and returns "Even" for even numbers or "Odd" for odd numbers.
★ Problem Breakdown ★
- The function needs to take an integer as input.
- The function must check if the number is even or odd.
- The function must return “Even” if the number is even or “Odd” if it’s odd.
In python, we can use the % operator that calculates the remainder of a division.
Even and Odd numbers:
- An even number is divisible by 2 without a remainder (2, 4, 6, ...).
- An odd number leaves a remainder of 1 when divided by 2 (1, 3, 5, ...).
★ Flowchart ★

★ Code ★
- We can use an if/else statement for checking whether a number is even or odd:
- A more concise way to write this is using a ternary conditional:
✰ღ★ღ Counting sheep ღ★ღ✰
Consider an array/list of sheep where some sheep may be missing from their place. We need a function that counts the number of sheep present in the array (true means present). For example:
Hint: Don't forget to check for bad values like null/undefined.
★ Problem Breakdown ★
- The problem has an array that contains True or False values.
- The function needs to count the number of sheep present in the array.
- True means present.
In python, we can use the count() method or we can iterate through each element in the array.
★ Flowchart ★
- Flowchart iterating:

The .count() method functions for itself, so doesn’t require a flowchart.
★ Code ★
- We call the .count() method, passing the argument “True” which counts the occurrences of the True value in the list:
- First, we initialize a counter at 0, then iterate through the list, incrementing it (count += 1) for each True value.
The reason we use if sheep instead of if sheep == True is that Python checks whether the value of sheep is 'truthy'. This is equivalent to writing if sheep == True, but it's shorter and cleaner.
- Autor:ByIris
- URL:http://145.223.74.189:3000//article/python-problems-l
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!