Q1: Class Definition
Write a Python class MyFitness that includes two public methods:
add_food(username, kind_of_food, amount)
Parameters:
username: A string representing the user's name.
kind_of_food: Can be either "protein", "fat", or "carbs".
amount: The quantity of the specified food in grams.
The method calculates the calories based on the type of food added:
Protein: 1 gram = 40 calories
Fat: 1 gram = 70 calories
Carbohydrates: 1 gram = 140 calories
The method updates the user's calorie and macro information.
show_information()
This method displays the current food intake information for all users in JSON format.
Example output:
json
Copy code
[
{"username": "user1", "protein": 140, "fat": 150, "carbs": 170, "calories": 300},
{"username": "user2", "protein": 150, "fat": 150, "carbs": 70, "calories": 290}
]
Q2: Password Validation Function
Write a boolean function is_password_valid(passfromDb, passfromWeb) that checks whether the password entered via the web (passfromWeb) can be accepted based on the following condition:
The passwords must be exactly the same except for one character.
The mistake in the web password can only occur if the character in passfromWeb is a neighboring key to the corresponding character in passfromDb on a standard QWERTY keyboard.
Example:
python
Copy code
passfromDb = "absd"
passfromWeb = "ansd"
In this case, the function would return True because 'n' is a neighboring key to 'b' on the keyboard.