Skip to content

Instantly share code, notes, and snippets.

@online
Last active September 23, 2019 21:43
Show Gist options
  • Save online/210266de2dc7a5abd200647dceb4c162 to your computer and use it in GitHub Desktop.
Save online/210266de2dc7a5abd200647dceb4c162 to your computer and use it in GitHub Desktop.
my_first_function.py (Python)
def discounted(price, discount): # это функция, которая считает скидку
price = abs(float(price)) # приводим всё к float и удаляем знаки минус / плюс
discount = abs(float(discount))
if discount >= 100: # если скидка больше или равно 100
price_with_discount = price # выводим просто цену
else:
price_with_discount = price - price * discount / 100
return price_with_discount # ничего не выведет на экран
# Создадим словарь
product = {
'name': 'Samsung Galaxy S10',
'stock': 8,
'price': 50000.0,
'discount': 50
}
# Добавим в словарь новый ключ и применив нашу функцию,
# чтобы записать в новый ключ результат функции
product['with_discount'] = discounted(product['price'], product['discount'])
print(product)
# получим:
# {'name': 'Samsung Galaxy S10', 'stock': 8, 'price': 50000.0, 'discount': 50, 'with_discount': 25000.0}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment