Wellbeing App
KNOWLEDGE
KNOWLEDGE
Output to the screen with formatting
In Python, \033 is an escape sequence representing the ASCII control character for the escape character. It is commonly used to control cursor movement and text formatting in terminal or console environments. However, formatting output this way may not be ideal for programmers as it can be hard to read, and changing the color for every single output can be tedious. Each time a color is changed, the text format remains that color until manually reset to default using \033[0m.
To address this, a solution is to create a function for changing specific text in the string to predefined colors, as demonstrated in the "colors" dictionary. Each color in the dictionary corresponds to its respective ASCII control character. The function takes two parameters: the text to be colored and the desired color. It concatenates the text with the respective color ASCII control character and resets the text back to default. Now, to print something in color, one simply needs to call the "colored()" function. An example of how I used it is provided on top.
Use of String methods
Using string methods is important because it allows for efficient manipulation and processing of text data in Python. In this case, the ".format()" method is utilized to format a string containing the time, accessed through the "new_hours" and "new_minutes" variables. The "{:02d}" specifier formats an integer with at least two characters, padding it with leading zeros if necessary, ensuring uniform output of the time.
Use of input from the user
Especially for a wellbeing app, user input enhances interactivity. In this case, input from the user is stored in the "scheduleGen_input" variable, which is then used to check if the user typed "*finished," indicating that they have entered all their activities and are ready to generate their schedule. This input is also scrutinized for errors: if the input is a whole number between 10 and the remaining time available for planning activities (stored in "minsLeft"). If the input meets the criteria, it is stored in the activity dictionary and utilized by the program to determine the time period for each activity.
Use of arithmetic/math operations
In the function "convert_minutes()" that I have created, the aim is to address repetitive code by converting the minutes specified in the parameters into a more human-readable string. Floor division is employed to calculate the number of hours by dividing the minutes by 60, while the "%" modulus operation is used to obtain the remaining minutes.
Use of decision statements (if/elif)
Due to the numerous decisions required within the app, the use of decision statements is essential. The provided example is nested within the main menu loop, where user input is processed using if/elif/else statements to determine the subsequent actions. If the input is "1", the program proceeds to the schedule generator; if it's "2", it goes to the breathing exercise program. If the input is "exit", the loop is terminated, and the program ends. Finally, an else statement is utilized to handle any unspecified inputs, displaying an error message to the user.
Use of nested-if
A nested if statement is utilized to make decisions within a decision. In this scenario, a three-layer nested if statement is employed. Initially, it checks if the user's answer input is "yes". Subsequently, it iteratively attempts to obtain a valid integer input from the user using a try-except-else block. If no error occurs, within the else block, another nested if statement is utilized to ascertain if the number of periods the user wants to break down falls between 2 (inclusive) and 10 (inclusive). If the input is not within this range, an error message is displayed, and the program continues to prompt the user for a valid input. Once a satisfactory input is provided, the program exits the while loop and proceeds with the correct input.
Use of logical operators (not, and, or)
Logical operators are pivotal for decision-making. The utilization of the "or" operator is demonstrated in the example. The "or" operator returns true if either "scheduleGen_input.isspace()" (checks if the input consists only of spaces) or "scheduleGen_input == ''" (indicating that the input is blank). If this condition is true, an error message is displayed to the user, informing them that they entered a blank activity name, which is not accepted by the program.
Use of while loop
A while loop is employed to continually prompt the user for valid input. The program retrieves input from the user, and if it is not valid, an error message is displayed, and the user is prompted to input again. If the input is deemed valid, the while loop is exited using the "break" statement.
Use of for loop
A for loop is utilized to count down from 3, preparing the user for the breathing exercises. It iterates 3 times, as defined by "range(3)", and with each iteration, the index "i" is incremented. This integer is then used to display the countdown for the user to observe. The breathing loop also employs a for loop to iterate as many times as the number of breaths specified by the user, see below.