write a program that will ask the user for 4 parameters: upper blood pressure(SYS),…
- User
write a program that will ask the user for 4 parameters: upper blood pressure(SYS), lower blood pressure(DIA), heart rate(PUL) and how it feels. And then write these parameters to a text file adding the current date and time
- Assistant
Here's a Python program that prompts the user for the parameters (upper blood pressure, lower blood pressure, heart rate, and feeling), records the current date and time, and then writes all the information to a text file:
import datetime # Prompting the user for parameters sys = input("Enter upper blood pressure (SYS): ") dia = input("Enter lower blood pressure (DIA): ") pul = input("Enter heart rate (PUL): ") feeling = input("Enter how you feel: ") # Getting the current date and time current_time = datetime.datetime.now() # Creating the record to be written in the file record = f"Date and time: {current_time}\nUpper blood pressure: {sys}\nLower blood pressure: {dia}\nHeart rate: {pul}\nFeeling: {feeling}" # Writing the record to a text file with open("health_records.txt", "a") as file: file.write(record) file.write("\n\n") print("Parameters recorded and saved successfully!")This program will ask the user to enter the upper and lower blood pressure, heart rate, and how they feel. It will then get the current date and time using the module. After that, it will create a record string that combines all the parameters and the current date and time. Finally, it will append this record to a text file named "health_records.txt" (creating the file if it doesn't exist) and print a success message.
- User
import datetime
Prompting the user for parameters
sys = input("Enter upper blood pressure (SYS): ") dia = input("Enter lower blood pressure (DIA): ") pul = input("Enter heart rate (PUL): ") feelings = input("Enter how you feel: ")
Getting the current date and time
current_time = datetime.datetime.now()
Creating the record to be written in the file
record = f"Date: {current_time} Pressure: {sys}/{dia} Heart rate: {pul} Health: {feelings}"
Writing the record to a text file
with open("health_records.txt", "a") as file: file.write(record) file.write("\n\n")
print("Parameters recorded and saved successfully!")
I have a code, make it so that it compares the current date with the last result written to a text file, if they are equal, then you don’t need to make empty lines at the end
- Assistant
To compare the current date with the last result written to the text file and determine whether or not to add empty lines at the end, you can modify the code as follows:
import datetime # Prompting the user for parameters sys = input("Enter upper blood pressure (SYS): ") dia = input("Enter lower blood pressure (DIA): ") pul = input("Enter heart rate (PUL): ") feelings = input("Enter how you feel: ") # Getting the current date and time current_time = datetime.datetime.now().date() # Creating the record to be written in the file record = f"Date: {current_time} Pressure: {sys}/{dia} Heart rate: {pul} Health: {feelings}\n" # Reading the last record from the file last_record = "" try: with open("health_records.txt", "r") as file: lines = file.readlines() if len(lines) > 0: last_record = lines[-1] except FileNotFoundError: pass # Writing the record to a text file with open("health_records.txt", "a") as file: if record.strip() != last_record.strip().split(" ")[0]: file.write("\n") file.write(record) print("Parameters recorded and saved successfully!")