Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import statistics
- user_stats = []
- def program_menu():
- print("Welcome message to the user to explain what the programm is going to do")
- choice = continue_choice()
- if choice.lower() == 'y':
- stats = collect_user_health_stats('y')
- print("there are ", len(stats), "in the data set.")
- print("The values as follows:", end=" ")
- print(stats)
- get_lowest(stats, 'age')
- get_highest(stats, 'age')
- get_average(stats, 'age')
- get_mean(stats, 'age')
- # get_mode(stats, 'age')
- get_range(stats, 'age')
- get_lowest(stats, 'weight')
- get_highest(stats, 'weight')
- get_average(stats, 'weight')
- get_mean(stats, 'weight')
- #get_mode(stats, 'weight')
- get_range(stats, 'weight')
- get_lowest(stats, 'height')
- get_highest(stats, 'height')
- get_average(stats, 'height')
- get_mean(stats, 'height')
- #get_mode(stats, 'height')
- get_range(stats, 'height')
- get_lowest(stats, 'bmi')
- get_highest(stats, 'bmi')
- get_average(stats, 'bmi')
- get_mean(stats, 'bmi')
- #get_mode(stats, 'bmi')
- get_range(stats, 'bmi')
- else:
- return
- def continue_choice():
- print("Would you like to continue? (Y/N)", end=" ")
- choice = str(input()).upper()
- print(choice)
- while choice != "Y" and choice != "N":
- print("invalid option:Would you like to continue ?(Y/N) . ")
- choice = str(input()).upper()
- return choice
- def collect_user_health_stats(choice):
- while choice == 'y':
- height = get_user_height()
- weight = get_user_weight()
- age = get_user_age()
- bmi = weight / height
- bmi = round((bmi / height),2)
- weight_category = weight_category(bmi)
- user_stats.append({'height': height, 'weight': weight, 'age': age, 'bmi': bmi, 'weight_category': weight_category})
- print("Do you want to enter stats for another person? (Y/N)", end=" ")
- choice = continue_choice()
- print(choice)
- collect_user_health_stats(choice.lower())
- return user_stats
- def get_user_height():
- height = get_height_input()
- param = get_height_measured_input()
- height = calculate_height(float(height), str(param))
- return height
- def get_height_input():
- print("Please enter person height!", end=" ")
- height = eval(input())
- if not height or type(height) not in (float, int):
- print("Invalid height entered!")
- get_height_input()
- return height
- def get_height_measured_input():
- print("Height measured in meters(m) or feet (ft) or inches (in) ?", end=" ")
- param = input()
- if not param or type(param) != str or str(param) not in ('m', 'meters', 'ft', 'feet', 'in', 'inches',):
- print("Invalid choice selected")
- get_height_measured_input()
- return param
- def calculate_height(height, param):
- if param == 'ft' or param == 'feet':
- return height / 3.2808 # 1 meter = 3.2808 feet
- elif param == 'in' or param == 'inches':
- return height / 39.370 # 1 meter = 39.3701 inch
- else:
- return height
- def get_user_weight():
- weight = get_weight_input()
- param = get_weight_measured_input()
- weight = calculate_weight(float(weight), str(param))
- return weight
- def get_weight_input():
- print("Please enter person weight!", end=" ")
- weight = eval(input())
- if not weight or type(weight) not in (float, int):
- print("Invalid weight entered!")
- get_weight_input()
- return weight
- def get_weight_measured_input():
- print("Weight measured in pounds(lb) or stone (st) or kilograms (kg) ?", end=" ")
- param = input()
- if not param or type(param) != str or str(param) not in ('lb', 'pounds', 'st', 'stone', 'kg', 'kilograms',):
- print("Invalid choice selected")
- get_weight_measured_input()
- return param
- def calculate_weight(weight, param):
- if param == 'lb' or param == 'pounds':
- return weight / 2.2046 # 1 kg = 2.2046 pounds
- elif param == 'st' or param == 'stone':
- return weight / 0.15747 # 1 kg = 0.15747 stone
- else:
- return weight
- def get_user_age():
- print("Please enter person age", end=" ")
- age = eval(input())
- if not age or type(age) not in (float, int):
- print("Invalid age entered!")
- get_user_age()
- return int(age)
- def weight_category(bmi):
- if bmi <= 18.5:
- print(" You are underweight.")
- elif bmi > 18.5 and bmi < 25:
- print(" You are Healty.")
- elif bmi > 25 and bmi < 30:
- print("You are overweight.")
- elif bmi > 30:
- print("You are obese.")
- else:
- print(" Ther is an error please try again")
- return weight_category
- def spread_stats(stats, key):
- return [x[key] for x in stats]
- def get_average(stats, key):
- # this function return average from list
- sum_total = sum(spread_stats(stats, key))
- avg = sum_total / len(spread_stats(stats, key))
- print("The average {key} in the list is :".format(key=key), avg)
- return round((avg),2)
- def get_lowest(stats, key):
- # this function return max from list
- value = min(spread_stats(stats, key))
- print("The lowest {key} entered is :".format(key=key), value)
- return value
- def get_highest(stats, key):
- # this function return max from list
- value = max(spread_stats(stats, key))
- print("The highest {key} entered is :".format(key=key), value)
- return value
- def get_mean(stats,key):
- numbers = spread_stats(stats, key)
- value = float(sum(numbers)) / max(len(numbers), 1)
- print("The mean {key} entered is :".format(key=key), value)
- return round((value),2)
- def get_mode(stats,key):
- numbers = spread_stats(stats, key)
- value = statistics.mode(numbers)
- print("The mode {key} entered is :".format(key=key), value)
- return round((value),2)
- def get_range(stats,key):
- numbers = spread_stats(stats, key)
- range = max(numbers) - min(numbers)
- print("The range {key} entered is : {minimum} - {maximum}".format(key=key,minimum = min(numbers), maximum = max(numbers)))
- return range
- program_menu()
Add Comment
Please, Sign In to add comment