Using Python 3.5!!! Question 2: Convert to Leap Year: Leap Years are defines as
ID: 3788161 • Letter: U
Question
Using Python 3.5!!! Question 2: Convert to Leap Year: Leap Years are defines as such: "There is a leap year every year whose number is perfectly divisible by four except for years which are both divisible by 100 and not divisible by 400. The second part of the rule effects century years. For example: the century years 1600 and 2000 are leap years, but the century years 1700, 1800, and 1900 are not.". Write a program that reads a file of years and writes back whether or not the year is a leap year after the number on the same line. This program asks the user for the name of the file and upon competition prints a short message announcing competition. The format of the input file is one year per line. Here is a file to use for your testing. The format of the output file, one per line (see sample output below): year (space) Leap Year Status. Required Design Scheme: Write a function isLeapYear(year, that takes in an integer year. Returns a string saying "Leap Year" or "Not a Leap Year". Write a function build, output(file), that takes in a file opened for reading. This function reads in the input file. Generates the corresponding line for the output file. Calls isLeapYear(year) as a part of the generation process. Stores the corresponding output lines in a list. Returns the list of output lines. Write a function build out(name) that takes in the name of the file. This function opens the file for reading. Calls build output file) Closes the file. Returns the list returned by build output file Write a function print to files data) that takes in a file open for writing file, and a list of lines to output. Loops over the list of output data- Writes out the appropriate data to the output file write a function write outl data) that takes in the name of a file and name, the data to output. Opens the file for writing Calls print to file, data) Closes the file Write a function called main0 Asks the user for the name of the file.Explanation / Answer
A leap year is nothing but exactly divisible by 4
# Python program to check if the year is a leap year or not
# To get year (integer input) from the user
#
x = int(input("what is the name of the file we want to open?")
year = int(input("Enter a year: "))
function isLeapYear(Year){
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
}
2) Rading a file by opening it using function
function build_output(year){
file = open(“year.txt”, “r”)
isLeapYear("user text")
print file.readlines(year):
}
3)function build_out (name){
h = open(“year.txt”,”r”) # opens file for reading
build_out(year)
lines = [“NON LEAP YEAR”, "Hello ALL LEapyear"]
}
fh.writelines(lines)
fh.close()
4) function print_to_file(file, data){
h = open(“year.txt”,”r”) # open file
for line in file:
print line, Writes out the data
}
5)function write_out(name,data)
h = open(“year.txt”,”r”) # open file
print_to_file (file, year.text)
fh.close()
}
6) int main() #main section for name of the program
{
int(input("Enter name of the file : ")
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.