Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Function basics
More advanced concepts
Summary

Instruction

Perfect! You can also invoke your functions inside other functions. Take a look:

def contains_digits(word):
  for letter in word:
    if letter.isnumeric():
      return True
  return False

def contains_letters(word):
  for letter in word:
    if letter.isalpha():
      return True
  return False

def validate_password(password):
  if not contains_letters(password) or not contains_digits(password):
    return False
  else:
    return True

First, we defined two functions that check whether (a) a given word contains digits and (b) a given word contains letters. Next, we used these functions inside another function that checks whether a user's password is in the proper format (i.e., whether it contains at least one letter and at least one digit).

Exercise

Create a function that will convert gross salary to net salary in an imaginary country. Name the function convert_gross_to_net(salary). The function should print the following information:

Your salary gross: {x}
Salary after social contribution: {y}
Net salary after tax: {z}

First, create two helper functions: calculate_social_contribution(amount) and calculate_tax(amount), and use them later in your main function.

The calculate_social_contribution(amount) function returns:

  • 0 for amounts below 200.
  • 100 for amounts greater than 200 and less than 1000.
  • 200 for amounts greater than or equal 1000.

The calculate_tax(amount) function should return the tax, which equals:

  • 10% of amount if the amount is up to 3000.
  • 300 + 20% of all the amount above 3000 if amount is greater than 3000.

Then, finally write the convert_gross_to_net(salary) function. The function should print the following information:

Your salary gross: {x}
Salary after social contribution: {y}
Net salary after tax: {z}

The following rules apply:

First, the social contribution is calculated. Once the social contribution has been deducted from the salary, you need to calculate and subtract the tax from the remaining amount. Use the helper functions to do it.

For instance, for salary = 500:

  • calculate_social_contribution(5000) should return 200.
  • 5000 - 200 = 4800
    calculate_tax(4800) should return 660.0 (300 + (4800 - 3000) * 20% = 660.0)
  • 4800 - 660.0 = 4140.0
    convert_gross_to_net(5000) should print:
    Your salary gross: 5000
    Salary after social contribution: 4800
    Net salary after tax: 4140.0