A veces estamos tan ocupados que ni siquiera somos capaces de cuidar nuestro cuerpo y por cuidar nos referimos a fitness, alimentación, ejercicios y mucho más, y entonces pensamos que necesitamos hacer nuestro plan de dieta o plan de ejercicios para mantener nuestro cuerpo. Así que hagamos un script de Python para mantener este registro para nosotros. En este programa, agregamos nuestra dieta y ejercicios que necesitamos hacer, o podemos guardar nuestra dieta diaria o ejercicio con fecha y hora para llevar un registro. También nos ayuda a hacer otro plan de acondicionamiento físico.
En este programa, el usuario ingresa su dieta y ejercicio, y luego guardamos la entrada con la fecha y la hora, para que cuando el usuario quiera pueda ver su rutina. Las siguientes operaciones se realizarán bajo este:
- obtener la fecha()
- seleccionarnombre()
- seleccionar_archivo_acción()
- seleccionar_tarea()
- acción()
Acercarse:
- getdate(): realiza un seguimiento de la fecha de los datos para que la recuperación del registro tenga sentido
Python3
def getdate(): # to get date and time return datetime.datetime.now()
- selectname(): Esta función permite al usuario seleccionar la persona sobre la que se realizarán las tareas requeridas. Esta función primero da opciones para elegir entre dos personas y toma la entrada como un número entero. Esto luego se devuelve para su posterior procesamiento.
Python3
def selectname(): name = {1: "Nilesh", 2: "Shanu"} b = {1: "Food", 2: "Exercise"} for key, value in name.items(): # taking input of name print("Press", key, "for", value, "\n", end="") n = int(input("type here..")) if n > 2: print("error select 1 or 2") exit() else: return n
- select_file_action(): Esta función acepta lo que debe hacerse con el archivo, es decir, el archivo debe usarse para escribir los datos (registro) o recuperar los datos existentes. Esto nuevamente toma argumentos enteros y los devuelve.
Python3
def select_file_action(): a = {1: "Log", 2: "Retrieve"} for key, value in a.items(): # taking input of function that user wants to # do (either log or retrieve) print("Press", key, "for", value, "\n", end="") x = int(input("type here..")) if x > 2: print("error select 1 or 2") exit() else: return x
- select_task(): esta función ayuda a especificar los datos relacionados con la tarea que se debe ingresar. La opción aquí dada es entre comida y ejercicio. Esto nuevamente acepta la entrada de números enteros y los devuelve para su posterior procesamiento.
Python3
def select_task(): b = {1: "Food", 2: "Exercise"} for key, value in b.items(): # ask user to choose between food and exercise print("Press", key, "for", value, "\n", end="") y = int(input("type here..")) if y > 2: print("error select 1 or 2") exit() else: return y
- action(): realiza la tarea adecuada de acuerdo con las entradas devueltas por las funciones anteriores mediante la verificación de condiciones.
Python3
def action(n, x, y): # condition no 1 if n == 1 and x == 1 and y == 1: value = input("type here\n") with open("nilesh food.txt", "a") as nileshfood: # printing date and time nileshfood.write(str([str(getdate())]) + ": " + value + "\n") print("successfully written") # condition no 2 elif n == 1 and x == 1 and y == 2: value = input("type here\n") # printing date and time with open("nilesh exercise.txt", "a") as nileshexercise: # printing date and time nileshexercise.write(str([str(getdate())]) + ": " + value + "\n") print("successfully written") # condition 3 elif n == 2 and x == 1 and y == 1: value = input("type here\n") # printing date and time with open("shanu food.txt", "a") as shanufood: # printing date and time shanufood.write(str([str(getdate())]) + ": " + value + "\n") print("successfully written") # condition 4 elif n == 2 and x == 1 and y == 2: value = input("type here\n") # printing date and time with open("shanu exercise.txt", "a") as shanuexercise: # printing date and time shanuexercise.write(str([str(getdate())]) + ": " + value + "\n") print("successfully written") # condition 5 elif n == 1 and x == 2 and y == 1: # printing date and time with open("nilesh food.txt", "r") as nileshfood: a = nileshfood.read() print(a) # condition no 6 elif n == 1 and x == 2 and y == 2: # printing date and time with open("nilesh exercise.txt", "r") as nileshexercise: a = nileshexercise.read() print(a) # condition no 7 elif n == 2 and x == 2 and y == 1: # printing date and time with open("shanu food.txt", "r") as shanufood: a = shanufood.read() print(a) # condition no 8 elif n == 2 and x == 2 and y == 2: # printing date and time with open("shanu exercise.txt", "r") as shanuexercise: a = shanuexercise.read() print(a)
A continuación se muestra una implementación completa.
Python
import datetime def getdate(): # to get date and time return datetime.datetime.now() def selectname(): name = {1: "Nilesh", 2: "Shanu"} b = {1: "Food", 2: "Exercise"} for key, value in name.items(): # taking input of name print("Press", key, "for", value, "\n", end="") n = int(input("type here..")) if n > 2: print("error select 1 or 2") exit() else: return n def select_file_action(): a = {1: "Log", 2: "Retrieve"} for key, value in a.items(): # taking input of function that user wants to # do (either log or retrieve) print("Press", key, "for", value, "\n", end="") x = int(input("type here..")) if x > 2: print("error select 1 or 2") exit() else: return x def select_task(): b = {1: "Food", 2: "Exercise"} for key, value in b.items(): # ask user to choose between food # and exercise print("Press", key, "for", value, "\n", end="") y = int(input("type here..")) if y > 2: print("error select 1 or 2") exit() else: return y def action(n, x, y): # condition no 1 if n == 1 and x == 1 and y == 1: value = input("type here\n") with open("nilesh food.txt", "a") as nileshfood: # printing date and time nileshfood.write(str([str(getdate())]) + ": " + value + "\n") print("successfully written") # condition no 2 elif n == 1 and x == 1 and y == 2: value = input("type here\n") # printing date and time with open("nilesh exercise.txt", "a") as nileshexercise: # printing date and time nileshexercise.write(str([str(getdate())]) + ": " + value + "\n") print("successfully written") # condition 3 elif n == 2 and x == 1 and y == 1: value = input("type here\n") # printing date and time with open("shanu food.txt", "a") as shanufood: # printing date and time shanufood.write(str([str(getdate())]) + ": " + value + "\n") print("successfully written") # condition 4 elif n == 2 and x == 1 and y == 2: value = input("type here\n") # printing date and time with open("shanu exercise.txt", "a") as shanuexercise: # printing date and time shanuexercise.write(str([str(getdate())]) + ": " + value + "\n") print("successfully written") # condition 5 elif n == 1 and x == 2 and y == 1: # printing date and time with open("nilesh food.txt", "r") as nileshfood: a = nileshfood.read() print(a) # condition no 6 elif n == 1 and x == 2 and y == 2: # printing date and time with open("nilesh exercise.txt", "r") as nileshexercise: a = nileshexercise.read() print(a) # condition no 7 elif n == 2 and x == 2 and y == 1: # printing date and time with open("shanu food.txt", "r") as shanufood: a = shanufood.read() print(a) # condition no 8 elif n == 2 and x == 2 and y == 2: # printing date and time with open("shanu exercise.txt", "r") as shanuexercise: a = shanuexercise.read() print(a) n = selectname() x = select_file_action() y = select_task() action(n, x, y)
Producción: