Basic Questions – Functions & While Statements

Question 1: Writing a Function
Writing functions serves a specific purpose and allows the user to sort what they want easily.
Task: Write a function square(n) that takes an integer as input, and returns the square of the integer.
square(3) -> 9
square(-2) -> 4
square(0) -> 0

Answer:

Explanation: Since ** means exponent in Python, we use **. A common mistake is the use of ^, which copies the bit if it is set in one operand but not both variables.

Question 2: While Statements
While statements provide a mechanism that allows repeated computations to be performed. The while statement has the form:
while condition:
    body_code

Task: Print the average of the entered numbers.
‘3’, ‘1.5’, ‘2’, ‘2.5’ -> ‘2.0’
‘1’, ‘0’ -> ‘0’

Answer:

Explanation: The first variable number_of_entries, requires the user to input how many numbers are being added. The next variable count, is made so that I can make a “count of the numbers”. The next variable total is the final number that the numbers we want to input total to.

Using the while statement, we make it so that it loops until there is zero numbers. Under the while statement, we need to float the number since we have decimals. The total variable then becomes the current total amount added with the floated number. The count variable is then subtracted by 1, because we already accounted for the first, second, … number. The final result is printed.

Question 3: While Using Break
Sometimes it is convenient to be able to break out of a while loop from within the body of the while loop using the break command. This is often the case when it is more sensible to test a condition in the body that is different from the while loop condition.
Task: Print the average of the entered numbers.

‘1.5’, ‘2’, ‘2.5’, ” -> 2.0

‘3.3’, ” -> 3.3

Answer:

Explanation: We make the count and total variable equal to 0, since no numbers have been added. A While Statement is used, and inside that statement, a variable num needs to be created so that we can input a number. An if and else statement is used after, where we need to make it so that the while Statement stops running if no number is added, and make it continue running when a number is added. The final result is printed when the While Statement is broken.

Question 4: Writing a Function Using While
Writing a function while using a while statement is incredibly useful when a loop is required (e.g. for large amounts of data to sort out).
Task: Write a function div_3_5(start, end) that computes the number of integers from start up to, but not including end that are divisible by 3 or 5 using a while loop. 
div_3_5(7, 27) -> 9
div_3_5(6, 27) -> 10
div_3_5(7, 28) -> 10
div_3_5(6,6) -> 0

Answer:

Explanation: We set a variable x as 0. A While Statement is used so that it loops, until the smaller number in the range (start) is smaller than than the larger number in the range (end). % is the modulus or the remainder after dividing by 3 or 5. Since we want the amount of numbers that is divisible by 3 or 5 (the numbers will have no remainder after being divided by 3 or 5), an if statement is required so that the count (x) increases by one. The lowest number in the range increase by one so that the number can be analysed in the while loop. After the while statement condition is not satisfied, the count (x) is printed and returned.

, , , , , , , , , , , , , , , , , , ,

No comments yet.

Leave a Reply

Powered by WordPress. Designed by WooThemes

Free WordPress Themes