-
Notifications
You must be signed in to change notification settings - Fork 2
/
bmi_except.py
42 lines (34 loc) · 1.02 KB
/
bmi_except.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""BMI function with exceptions."""
def is_number(number):
"""Check if is number."""
if isinstance(number, (float, int)):
return number
if not isinstance(number, str):
return False
number = number.replace(',', '.')
try:
return float(number)
except ValueError:
return False
def get_user_data():
"""User data function."""
user_data = {}
weight = is_number(input('Type weight: '))
while weight is False:
print('Weight is not number.')
weight = is_number(input('Type weight: '))
user_data['weight'] = weight
height = is_number(input('Type height: '))
while height is False:
print('Height is not number.')
height = is_number(input('Type height: '))
user_data['height'] = height
return user_data
def bmi():
"""BMI function."""
user_data = get_user_data()
bmi = round(user_data['weight'] / user_data['height'] ** 2, 2)
print('Your BMI is: %s' % (bmi))
bmi()