Text file won't open with open() - python

I need help with this exercise please. I have to ask the user if want to set an alarm, either for a specific date or for certain days of the week. In addition to printing the time adding 1 and I have to say if it is day or night (This last part of the code was made by the teacher of the course, I did all the alarm).
The code should open a .txt file but it does not, I have run the code several times and checked the Pycharm but nothing. Here it is
from time import sleep
import datetime
NIGHT_STARTS = 19
DAY_STARTS = 8
HOUR_DURATION = 1
def write_file_and_screen(text, file_name):
with open(file_name, "a+") as file_text:
file_text.write("{}{}".format(text, "\n"))
print(text)
def week_date_to_day(day):
days_list = {0: "Monday", 1: "Tuesday", 2: "Wednesday", 3: "Thursday", 4: "Friday", 5: "Saturday",
6: "Sunday"}
day_weekday = day.weekday
if day_weekday == days_list.keys():
week_day_of_the_day = days_list.get(day_weekday)
return week_day_of_the_day
def main():
today = datetime.date.today()
current_time = datetime.datetime.now()
is_night = False
want_alarm = input("Do you want to set a alarm? Yes/No ")
if want_alarm == "Yes":
# Aquí preguntamos si desea una alarma para una fecha específica.
specific_date = input("Do you want an alarm in a specific date? Yes/No ")
if specific_date == "Yes":
date_user = input("Tell me the date. (dd/mm/yyyy) ")
date_format = datetime.datetime.strptime(date_user, "%d/%m/%Y")
if datetime.date.today() == date_format:
write_file_and_screen("ALARM. It's {}".format(date_format), "Specific alarm.txt")
print("ALARM. It's {}".format(date_format))
elif specific_date == "No":
# Aquí preguntamos si desea una alarma normal, haciendo que elija el día y la hora.
normal_alarm = input("Do you want a normal alarm? Yes/No ")
if normal_alarm == "Yes":
hour_alarm = int(input("Hour of the alarm? 0/23 "))
datetime.time(hour=hour_alarm)
days_of_alarm_input = ""
days_of_alarm_list = []
print("Write End to end the loop ")
while not days_of_alarm_input == "End":
days_of_alarm_input = input("Tell me the days that you want to set the alarm 0 to 6, 0 is "
"Monday ""and 6 is Sunday ")
days_of_alarm_list.append(days_of_alarm_input)
if days_of_alarm_input == "End":
for i in days_of_alarm_list:
if today.weekday() == days_of_alarm_list:
write_file_and_screen("ALARM. It's {}".format(week_date_to_day(today)), "Weekdays "
"alarm.txt")
while True: # Se imprime la hora actual y se le va sumando una más, además de que si indica si es de día
# o de noche
sleep(HOUR_DURATION)
current_time += datetime.timedelta(hours=1)
light_changed = False
if (current_time.hour >= NIGHT_STARTS or current_time.hour <= DAY_STARTS) and not is_night:
is_night = True
light_changed = True
elif (DAY_STARTS < current_time.hour < NIGHT_STARTS) and is_night:
is_night = False
light_changed = True
if light_changed:
if is_night:
write_file_and_screen("It's night", "hours.txt")
else:
write_file_and_screen("It's day", "hours.txt")
write_file_and_screen("The hour is {}".format(current_time), "horas.txt")
sleep(2)
if __name__ == "__main__":
main()
When I run the program and I enter the necessary data for the alarm, simply the program goes to the 3rd part of the code and starts printing the time and adding 1 without opening the .txt file:
The hour is 2019-11-09 19:50:51.614472
The hour is 2019-11-09 20:50:51.614472
The hour is 2019-11-09 21:50:51.614472
The hour is 2019-11-09 22:50:51.614472
The hour is 2019-11-09 23:50:51.614472
The hour is 2019-11-10 00:50:51.614472
The hour is 2019-11-10 01:50:51.614472
The hour is 2019-11-10 02:50:51.614472
The hour is 2019-11-10 03:50:51.614472
The hour is 2019-11-10 04:50:51.614472
The hour is 2019-11-10 05:50:51.614472
If you need to know something else or If I did not explain myself well please tell me. (Sorry for my English)
Update:
The specific date alarm works! Thanks!
but there is another problem. The "normal alarm" does not. When the program is on the line for i in days_of_alarm_list: it skip it to while True
i = 6 , it suppose to pass the for i in days_of_alarm_list:
But the program passes to while True

There was a type mismatch earlier, that's why you were going to the third part every time.
datetime.datetime.strptime('10/11/2019',"%d/%m/%Y") returns
'datetime.datetime(2019, 11, 10, 0, 0)'
datetime.date.today() returns 'datetime.date(2019, 11, 10)'
replace your if condition with the following line:
if datetime.date.today() == date_format.date() :

Your code working fine, I tested it on my machine,
In your code you'r sleeping your condition for 1 hour every time, that's why your code taking more time for execute file creation, modification.
Exactly waiting for 1 Hour every time when while condition looping
sleep method is
sleep(HOUR_DURATION)
You can check it by changing the time duration from sleep(HOUR_DURATION) to sleep(5)
Your condition will sleep just for five milliseconds
Happy codding

Related

KeyError when trying to access the mode of DataFrame columns

I am trying to run the following code:
import time
import pandas as pd
import numpy as np
CITY_DATA = {'chicago': 'chicago.csv',
'new york city': 'new_york_city.csv',
'washington': 'washington.csv'}
def get_filters():
"""
Asks user to specify a city, month, and day to analyze.
Returns:
(str) city - name of the city to analyze
(str) month - name of the month to filter by, or "all" to apply no month filter
(str) day - name of the day of week to filter by, or "all" to apply no day filter
"""
print('Hello! Let\'s explore some US bikeshare data!')
# get user input for city (chicago, new york city, washington). HINT: Use a while loop to handle invalid inputs
while True:
city = input('Which city you would like to explore : "chicago" , "new york city" , or "washington" :' )
if city not in ('chicago', 'new york city', 'washington'):
print(" You entered wrong choice , please try again")
continue
else:
break
# get user input for month (all, january, february, ... , june)
while True:
month = input('Enter "all" for all data or chose a month : "january" , "february" , "march", "april" , "may" or "june " :')
if month not in ("all", "january", "february", "march", "april", "may", "june"):
print(" You entered wrong choice , please try again")
continue
else:
break
# get user input for day of week (all, monday, tuesday, ... sunday)
while True:
day = input('Enter "all" for all days or chose a day : "saturday", "sunday", "monday", "tuesday", "wednesday", "thursday", "friday": ')
if day not in ("all","saturday", "sunday", "monday", "tuesday", "wednesday", "thursday", "friday"):
print(" You entered wrong choice , please try again")
continue
else:
break
print('-'*60)
return city, month, day
def load_data(city, month, day):
"""
Loads data for the specified city and filters by month and day if applicable.
Args:
(str) city - name of the city to analyze
(str) month - name of the month to filter by, or "all" to apply no month filter
(str) day - name of the day of week to filter by, or "all" to apply no day filter
Returns:
df - Pandas DataFrame containing city data filtered by month and day
"""
df = pd.read_csv(CITY_DATA[city])
# convert the Start Time column to datetime
df['Start Time'] = pd.to_datetime(df['Start Time'])
# extract month , day of week , and hour from Start Time to new columns
df['month'] = df['Start Time'].dt.month
df['day_of_week'] = df['Start Time'].dt.day_name
df['hour'] = df['Start Time'].dt.hour
# filter by month if applicable
if month != 'all':
# use the index of the month_list to get the corresponding int
months = ['january', 'february', 'march', 'april', 'may', 'june']
month = months.index(month) + 1
# filter by month to create the new dataframe
df = df[df['month'] == month]
# filter by day of week if applicable
if day != 'all':
# filter by day of week to create the new dataframe
df = df[df['day_of_week'] == day.title()]
return df
def time_stats(df):
"""Displays statistics on the most frequent times of travel."""
print('\nCalculating The Most Frequent Times of Travel...\n')
start_time = time.time()
# display the most common month
popular_month = df['month'].mode()[0]
print('\n The most popular month is : \n', popular_month)
# display the most common day of week
popular_day = df['day_of_week'].mode()[0]
print('\n The most popular day of the week is : \n', str(popular_day))
# display the most common start hour
popular_hour = df['hour'].mode()[0]
print('\n The most popular hour of the day is :\n ', popular_hour)
print("\nThis took %s seconds.\n" % (time.time() - start_time))
print('-'*60)
def station_stats(df):
"""Displays statistics on the most popular stations and trip."""
print('\nCalculating The Most Popular Stations and Trip...\n')
start_time = time.time()
# display most commonly used start station
start_station = df['Start Station'].value_counts().idxmax()
print('\n The most commonly used start station is : \n', start_station)
# display most commonly used end station
end_station = df['End Station'].value_counts().idxmax()
print('\nThe most commonly used end station is: \n', end_station)
# display most frequent combination of start station and end station trip
combination = df.groupby(['Start Station','End Station']).value_counts().idxmax()
print('\nThe most frequent combination of start station and end station are: \n', combination)
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def trip_duration_stats(df):
"""Displays statistics on the total and average trip duration."""
start_time = time.time()
travel_time = sum(df['Trip Duration'])
print('Total travel time:', travel_time / 86400, " Days")
# display total travel time
total_time = sum(df['Trip Duration'])
print('\nThe total travel time is {} seconds: \n', total_time)
# display mean travel time
mean_time = df['Trip Duration'].mean()
print('\n The average travel time is \n', mean_time)
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def user_stats(df):
"""Displays statistics on bikeshare users."""
print('\nCalculating User Stats...\n')
start_time = time.time()
# TO DO: Display counts of user types
user_types = df['User Type'].value_counts()
#print(user_types)
print('User Types:\n', user_types)
# TO DO: Display counts of gender
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def main():
while True:
city, month, day = get_filters()
df = load_data(city, month, day)
time_stats(df)
station_stats(df)
trip_duration_stats(df)
user_stats(df)
restart = input('\nWould you like to restart? Enter yes or no.\n')
if restart.lower() != 'yes':
break
if __name__ == "__main__":
main()
and I am receiving the following errors , can someone assist please
the errors:
> Traceback (most recent call last):
File "C:\Users\DELL\PycharmProjects\Professional\venv\Lib\site-packages\pandas\core\indexes\range.py", line 391, in get_loc
return self._range.index(new_key)
^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: 0 is not in range
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\DELL\PycharmProjects\Professional\Bikeshare.py", line 203, in <module>
main()
File "C:\Users\DELL\PycharmProjects\Professional\Bikeshare.py", line 192, in main
time_stats(df)
File "C:\Users\DELL\PycharmProjects\Professional\Bikeshare.py", line 100, in time_stats
popular_month = df['month'].mode()[0]
~~~~~~~~~~~~~~~~~~^^^
File "C:\Users\DELL\PycharmProjects\Professional\venv\Lib\site-packages\pandas\core\series.py", line 981, in __getitem__
Calculating The Most Frequent Times of Travel...
return self._get_value(key)
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\DELL\PycharmProjects\Professional\venv\Lib\site-packages\pandas\core\series.py", line 1089, in _get_value
loc = self.index.get_loc(label)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\DELL\PycharmProjects\Professional\venv\Lib\site-packages\pandas\core\indexes\range.py", line 393, in get_loc
raise KeyError(key) from err
KeyError: 0
I am expecting to filter pandas DataFrame to return month, day of week, and hour to perform some statistics.
KeyError means that the key isn't valid, because it doesn't exist. In this case, one reason to get KeyError when trying to get first mode is when column 'month' in dataframe is empty, and therefore mode() returns an empty collection, so you get KeyError: 0 when trying to get its first element.
To avoid this, you could replace:
popular_month = df['month'].mode()[0]
With:
try:
# try to get first mode of column 'month'
popular_month = df['month'].mode()[0]
except KeyError:
# if there's no data on column 'month'
popular_month = "unknown"
Because if there's no data on 'month' column, there's no point in trying to get its mode.
More about handling exceptions: https://docs.python.org/3/tutorial/errors.html#handling-exceptions
Also when I tried to ( not use the filters) by choosing " all " in the second and 3rd input, I get the following result:
Calculating The Most Frequent Times of Travel...
The most popular month is :
6
The most popular day of the week is :
<bound method PandasDelegate._add_delegate_accessors.._create_delegator_method..f of <pandas.core.indexes.accessors.DatetimeProperties object at 0x0000022B7CD5E890>>
The most popular hour of the day is :
17
This took 0.0260775089263916 seconds.
Calculating The Most Popular Stations and Trip...
The most commonly used start station is :
Streeter Dr & Grand Ave
The most commonly used end station is:
Streeter Dr & Grand Ave
The most frequent combination of start station and end station are:
('2112 W Peterson Ave', '2112 W Peterson Ave', 1064651, Timestamp('2017-06-02 07:59:13'), '2017-06-02 08:25:42', 1589, 'Subscriber', 'Female', 1963.0, 6, <bound method PandasDelegate._add_delegate_accessors.._create_delegator_method..f of <pandas.core.indexes.accessors.DatetimeProperties object at 0x0000022B7CD5E890>>, 7)
This took 2.1254045963287354 seconds.
Total travel time: 3250.8308680555556 Days
The total travel time is {} seconds:
280871787
The average travel time is
936.23929
This took 0.06502270698547363 seconds.
Calculating User Stats...
User Types:
Subscriber 238889
Customer 61110
Dependent 1
Name: User Type, dtype: int64
This took 0.022009611129760742 seconds.
Would you like to restart? Enter yes or no.

Whats wrong with this code for checking age?

I want to know if inputed date of birth is over 18 or under.
def is_under_18(birth):
now = date.today()
return (
now.year - birth.year < 18
or now.year - birth.year == 18 and (
now.month < birth.month
or now.month == birth.month and now.day <= birth.day
)
)
And then:
year = int(input("Year born: "))
month = int(input("Month born: "))
day = int(input("Day born: "))`
birth = date(year,month,day)
if is_under_18(birth):
print('Under 18')
else:
print('Adult')
However, the only thing is, say I add a user which his birthday is the 25th of November 2004. The program lets me add it because it does not count the month. If I add a user which was born the 1st of January 2005, it doesn't allow me because 2022-2005=17.
Your original code doesn't seem to have a problem with the dates you mention, but does have a bug as Nov 22, 2004 is "Under 18" and today's date is Nov 22, 2022 (18th birthday). Use now.day < birth.day instead.
But if you compute the birthday required to be 18 by replacing today's year with 18 less, then directly compare the dates, you don't have to have a complicated comparison:
from datetime import date
def is_under_18(birth):
# today = date.today()
today = date(2022,11,22) # for repeatability of results
born_on_or_before = today.replace(year=today.year - 18)
return birth > born_on_or_before
print(f'Today is {date.today()}')
for year,month,day in [(2004,11,21), (2004,11,22), (2004,11,23), (2004,11,25), (2005,1,1)]:
birth = date(year,month,day)
if is_under_18(birth):
print(f'{birth} Under 18')
else:
print(f'{birth} Adult')
Output:
Today is 2022-11-22
2004-11-21 Adult
2004-11-22 Adult
2004-11-23 Under 18
2004-11-25 Under 18
2005-01-01 Under 18

How can i sort data from a txt?

I need to sort this data chronologically, hopefully, by date and them by time, but right now I just want to sort it by date... the information is on a TXT file:
2022/5/10 at 10 the client Mari has appointment with the Dra. Windrunner
2022/1/5 at 2 the client Ian has appointment with the Dr. Stark
2022/1/4 at 10 the client Amy has appointment with the Dra. Windrunner
2022/1/5 at 2 the client Josh has appointment with the Dr. Stark
2022/2/22 at 5 the client Mike has appointment with the Dr. Pool
2022/2/22 at 4 the client Pedro has appointment with the Dr. Stark
This is my code right now:
Docs = ("Dr. Stark", "Dra. Windrunner", "Dr. Pool")
x = 0
loop = False
DocsConverter = {
"0" : "Dr. Stark",
"1" : "Dra. Windrunner",
"2" : "Dr. Pool",
"dr. stark": "Dr. Stark",
"dra. windrunner" : "Dra. Windrunner",
"dr. pool" : "Dr. Pool",
"stark" : "Dr. Stark",
"windrunner" : "Dra. Windrunner",
"pool" : "Dr. Pool"
}
with open("appointment_hospital.txt", "a") as file_ap:
pass
def menu():
option = input("select a option 1. new appointment 2.show appointments 3. Exit: (1/2/3)\n")
if option == "1":
new_appointment()
elif option == "2":
print_appointments()
elif option == "3":
file_ap.close()
exit()
else:
print("Wrong option")
def new_appointment():
global x
name_client = input("Enter the name of the client:\n")
schedule_day = input("Enter the year, month and the day of the appointment:(Y/M/D)\n")
schedule_time= input("Enter at what hour is the appointment:\n")
while x != 3:
print(f"{Docs[x]}, id = {x}")
x += 1
x = 0
which_doc = input("Enter the name or the Id of the doctor: ")
appointment_info = f"{schedule_day} at {schedule_time} the client {name_client} has appointment with the " \
f"{DocsConverter.get(which_doc)}\n"
with open("appointment_hospital.txt", "a") as file:
file.write(appointment_info)
#this is where i tried to sort the information in the txt
def print_appointments():
with open("appointment_hospital.txt", "r") as file:
lines_appointments = []
for line in file:
temp = line.split()
for i in temp:
lines_appointments.append(i)
lines_appointments.sort()
with open("sort_appointments", "w") as sort_file:
for i in lines_appointments:
sort_file.writelines(i)
sort_file.writelines(" ")
sort_file.close()
with open("sort_appointments", "w") as sort_file:
read_appointments = sort_file.read()
print(read_appointments)
while not loop:
menu()
So in def print_appointments(): I tried to sort the data, and this was my last tried, None of the ones I have done have given me a moderately positive result.
You have some mistakes:
no file extension open("sort_appointments"
to sort by dates residual split the file by lines
when writing a list to a file, you need to open it for appending "a"
when reading a file, you must put the letter "r"
def print_appointments():
with open("appointment_hospital.txt", "r") as file:
lines_appointments = []
for line in file:
lines_appointments.append(line)
lines_appointments.sort()
with open("sort_appointments.txt", "a") as sort_file:
sort_file.writelines(lines_appointments)
with open("sort_appointments.txt", "r") as sort_file:
read_appointments = sort_file.read()
print(read_appointments)
print_appointments()

How can i make this script more efficient?(Python)

im currently learning Python(about 3 months expirience now) and wanted to try out the module "Mouse", ive built a simple autoclicker script(not using it in game) but for values above 12(cps) it reaches below targeted cps, i suspect this is because of the if loops in my program, could anybody help me make this more efficient?
Code here:
import mouse
import keyboard
import time
staat = 0
cps = input("Typ de hoeveelheid cps die je wilt in.")
cps = float(cps)
cps = 1 / cps
print("pauzeer de loop door op / te drukken")
while True:
if staat == 1:
mouse.click('left')
if keyboard.is_pressed('/'):
if staat == 0:
staat = 1
else:
staat = 0
time.sleep(cps)
Thanks in advance
Using 1 instead of True is slightly faster. Importing this way is slightly faster and calling without the '.' is also slightly faster. If still not fast enough I could implement multi-threading .
from mouse import click
from keyboard import is_pressed
from time import sleep,perf_counter
staat = 0
cps = input("Typ de hoeveelheid cps die je wilt in.")
cps = float(cps)
cps = 1 / cps
print("pauzeer de loop door op / te drukken")
while 1:
timeing = perf_counter()
if staat:
click('left')
if is_pressed('/'):
if not staat:
staat = 1
else:
staat = 0
sleep(cps - (perf_counter() - timeing)

Not able to figure how to write if-else statement for one of the user defined cases in python

I have two API.
Australia API- This API works only for year 1985 to 2024.
USA API- I wanted this API need should work only before 1985.
Taking 4 things from user.
-Start Year
-End Year
-latitude
-longitude
sample command: python test.py -latitude '' -longitude '' -startYear '' -endYear ''
User can enter 3 ways of input.
Case 1. Start year=before 1985, end year= After 1985 ---->both AUSTRALIA and USA api run.
Case 2. Start year=At 1985 or later, end year= after 1985 ---->only AUSTRALIA api should run.
Case 3. Start year=before 1985, end year=before 1985 ------>only USA api run
Problem is that I am not able to figure out how to write the code for Case 1 after writing the code for case 2(Australia API) and case 3(USA API).
import requests
import json
import argparse
import time
import pandas as pd
import warnings
warnings.filterwarnings("ignore")
parser = argparse.ArgumentParser(description="Process some integers.")
parser.add_argument("-latitude", help="Latitude(Degress)")
parser.add_argument("-longitude", help="Longitude(Degress)")
parser.add_argument("-startYear", help="Start of the Year")
parser.add_argument("-endYear", help="End of the Year")
parser.add_argument("--verbose", help="display processing information")
start = time.time()
def main(latitude,longitude,startYear,endYear,verbose):
parameters = {
"latd": latitude, # [deg]
"latm": 00, # [deg]
"lats": 00, # [deg]
"lond": longitude, # [deg]
"lonm": 00, # [deg]
"lons": 00, # [deg]
"elev" : 00, # [km]
"year" : None, # [YYYY]
"month" : '07', # [MM]
"day": '01', # [DD]
"Ein": 'D' # [Model]
}
hostname = "https://api.geomagnetism.ga.gov.au/agrf"
hostname1 = "http://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination?%s"
df_1=pd.DataFrame()
for year in range(startYear, endYear):
if (startYear>=1985 and endYear>1985):
-----
elif (startYear<1985 and endYear<1985):
-------
if endYear < 1985:
if startYear < 1985:
# Case 3
elif startYear >= 1985:
# Case 2
elif startYear < 1985:
# Case 1
else:
# Case where endYear < 1985 and startYear > 1985 (probably an input error)
if end > 1985:
australia
if start < 1985:
usa
else:
usa

Categories