Tips for Creating Functions in Python

  1. Function names must be verbs.
  2. The name of each function should accurately reflect what the function does.
  3. A function should do only what its name clearly implies, and nothing else.
  4. Each function should perform only one action.
  5. Use helper functions inside functions.
  6. It is better not to make the function code longer than 10-15 lines.
  7. It is better to break long functions into a number of auxiliary ones.
  8. Use common prefixes in function names: show, get, set, calc, create, change, check.
  9. If a function name consists of two or more words, they should be separated from each other by an underscore _, for example, get_sum.
  10. Move duplicate code into functions.
Задание 1

Write down the shortcomings of the following code and fix them:

pythondef func(num1, num2):
  return num1 * num2
Задание 2

Write down the shortcomings of the following code and fix them:

pythondef user(name):
  return 'bye, ' + name
Задание 3

Write down the shortcomings of the following code and fix them:

pythondef get_num(num):
  return str(num)
Задание 4

Write down the shortcomings of the following code and fix them:

pythondef check(lst):
  sum = 0
  
  for el in lst:
    if el > 0:
      sum += el
    if el < 0:
      continue
    else:
      continue
      
  return sum