#!/usr/bin/env python3
#The leap_year() function will take a year in "YYYY" format, and return True if the given year is a leap year, otherwise return False.
The range_check() function will take an integer object and a tuple with two integer values, the first value indicates the lower bound and the second one indicates the upper bound of a integer range. If the integer object falls in between the range given in the tuple, return 'True', otherwise return 'False'.
The sanitize() function will take two string objects, the first string object is the object to be sanitized, and the 2nd string object contains letters that are allowed. This function will return the first object with letters not in the 2nd string object removed.
The size_check() function will take an collection data type object and expected number of items as an integer and will return either 'True' or 'False'. If the number of items in the data object match the integer value given, return 'True', otherwise return 'False'
The usage() function will take no argument and return a string describing the usage of the script.
# Imports the sys module
import sys
# usage function
# Shows the user how they should use the program properly
def usage():
'''
Help on function usage in module a1_szrahman3:
leap_year() prints out an error message if an error has occured
e.g. usage() -> 'Usage: a1_szrahman3 [--step] YYYYMMDD|YYYY/MM/DD|YYYY-MM-DD|YYYY.MM.DD +/-n'
(END)
'''
return "Usage: a1_szrahman3.py [--step] YYYYMMDD|YYYY/MM/DD|YYYY-MM-DD|YYYY.MM.DD|YYYY/MM/DD|YYYY-MM-DD|YYYY.MM.DD +/-n"
# range_check function
# It goes through an exception checking
# whether or not its ran properly or not
# If the arguments were not given properly
# it calls the usage function
# Otherwise it goes to a loop where it calls
# Either the tomorrow, or the yesterday function
def range_check(date, day):
"""
Help on function range_check in module a1_szrahman3:
range_check(date, day) -> str/int
range_check(date, day) takes YYYYMMDD|YYYY/MM/DD|YYYY-MM-DD|YYYY.MM.DD|YYYY/MM/DD|YYYY-MM-DD|YYYY.MM.DD and a positive or negative interger to
call the other functions in order to do the
calculations, while also calling size_check to determine if the date
is valid or not.
e.g. range_check(20200320, 5) -> 20200325
range_check(20200320, -5) -> 20200315
(END)
"""
error = usage()
if len(date) != 8:
return "Error 09: wrong date entered"
sys.exit()
try:
num = int(day)
tmpdate = date
valid = size_check(date)
if valid == False:
sys.exit()
else:
while num != 0:
if num > 0:
tmpdate = tomorrow(tmpdate)
num = num - 1
else:
tmpdate = yesterday(tmpdate)
num = num + 1
error = usage()
tar = tmpdate
return tar
except ValueError:
return error
# sanitize function
# While it also checks if it is a leap year
# It also stores the max day in a tuple
def sanitize(tmp_year):
"""
Help on function sanitize in module a1_szrahman3:
sanitize(tmp_year) -> int
sanitize() takes the year and calls the
leap_year function which will determine the ,
max number of days for February
e.g: sanitize(2020) -> 2:29
sanitize(2019) -> 2:28
(END)
"""
leapyear = tmp_year
check = leap_year(leapyear)
if check == True:
febmax = 29
elif check == False:
febmax =28
month_max = {1: 31, 2: febmax, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31,
9: 30, 10: 31, 11: 30, 12: 31}
return month_max
# leap_year function
# Check if year is leapyear
# If it is, returns as True
# If not, returned as False
def leap_year(leap):
"""
Help on function leap_year in module a1_szrahman3:
leap_year(leap) -> int
leap_year() determines if the year is a leap year
which will return as True or False
e.g: leap_year(2020) -> True
leap_year(2019) -> False
(END)
"""
l_year = leap
if l_year % 4 == 0:
return True
elif l_year % 100 == 0:
return False
elif l_year % 400 == 0:
return True
else:
return False
# size_check function
# Strips the variable
# Then runs through
# Statements to see if valid
# If valid, returns as True
# If not, calls one of the functions to
# Print out the error
# While elso returning a False Statement
def size_check(date):
"""
Help on function size_check in module a1_szrahman3:
size_check(date) -> str
size_check() determines if the date given is a valid date
or not, which returns True or False (which also prints out a message)
e.g: size_check(20200314) -> True
size_check(20200231) -> False -> 'Error 03: wrong day entered'
size_check(20201324) -> False -> 'Error 02: wrong month entered'
(END)
"""
y = int(date[0:4])
m = int(date[4:6])
d = int(date[6:])
tmp_d = d
tmp_m = m
sanitize = sanitize(y)
if tmp_m > 12 or tmp_m < 1:
print("Error 02: wrong month entered")
return False
else:
if tmp_d > sanitize[m]:
print("Error 03: wrong day entered")
return False
else:
return True
if __name__ == "__main__":
if len(sys.argv) == 3:
d = sys.argv[1]
n = sys.argv[2]
steparg = False
print(range_check(d, n))
elif len(sys.argv) == 4:
s = sys.argv[1]
d = sys.argv[2]
n = sys.argv[3]
if s == "--step":
steparg = True
range_check(d, n)
else:
print(usage())
else:
print((usage()))
python coding error
|
python coding error
|
|
Oct-17-2020, 06:30 AM
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info. Also, please, provide information, incl. full traceback in error tags
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs |
|
|
| Possibly Related Threads… | |||||
| Thread | Author | Replies | Views | Last Post | |
| Coding error. | xflyerwdavis | 2 | 1,633 |
Oct-07-2023, 07:08 PM Last Post: deanhystad |
|
| Coding error. Can't open directory | EddieG | 6 | 3,260 |
Jul-13-2023, 06:47 PM Last Post: deanhystad |
|
| Coding Error | EddieG | 2 | 1,622 |
Jul-09-2023, 02:59 AM Last Post: EddieG |
|
| Coding error- Not sure where I have put error markers against the code that is wrong | Username9 | 1 | 2,683 |
Sep-28-2020, 07:57 AM Last Post: buran |
|
| coding error iro a menu | Rollo | 2 | 3,326 |
Sep-27-2020, 04:17 PM Last Post: deanhystad |
|
| Adafruit LIS3DH coding error | FitDad73 | 1 | 3,569 |
Aug-30-2020, 01:37 AM Last Post: bowlofred |
|
| Coding error- help | FatherYeals | 3 | 61,940 |
Mar-27-2020, 02:11 PM Last Post: deanhystad |
|
| name undefined Coding Error | Ewilliam51 | 2 | 3,397 |
Feb-06-2020, 12:19 AM Last Post: Ewilliam51 |
|
| coding error | bilawal | 11 | 9,353 |
Dec-07-2019, 05:23 PM Last Post: DreamingInsanity |
|
| Pyhton Coding Help Needed (very small error) | friduk | 2 | 3,784 |
Oct-23-2019, 08:41 AM Last Post: Gribouillis |
|
Users browsing this thread: 1 Guest(s)
