Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need serious help.
#1
So I'm more of an intermediate programmer in Java, but I've had to move to a new area in the middle of being in school and am now learning Python in my current class. Things have been going fine, but to make things short, the class has picked up pretty rapidly and I'm confused on this long Assignment description...

Quote:*** CSC 201 – Program Assignment: Files, Exceptions and Functions ***

MAIN DESCRIPTION --> Write a program to read in a file of years (1583 <= year < 10,000), one per line, and output to a separate file only those years which are in fact leap years (according to the Gregorian calendar). Ask the user to input the file names of both files, and use the try-except construct when you open each in your program. <-- MAIN DESCRIPTION

The input file might contain headings and/or blank lines. Ignore them. A year might contain typographical errors, such as, 2oo1 or 2lOl. Reject it. A line might contain a comment after the four-digit year. Reject the comment, but process the year.

As part of your solution, you must write (at least) two functions. Remember the rule: A function should do one thing.

*** Program Development & Tests ***

Note that this problem has two independent parts: reading/writing files, and testing whether a year meets certain criteria. I recommend that you solve the problems in two parts. First, do not read or write data to or from any file. Instead, write two functions and test each by embedding the test data in your program. As a minimum, devise at least nine hand-worked solutions (strings) to test the following conditions:
1. The digits of a “year string” is greater than four.
2. The four-character string contains characters other than digits.
3. The string could be a four-digit year, but it is not in the correct range.
4. The year is a non-leap year by virtue of it being an odd number.
5. An even-numbered year that is not a leap year.
6. A year ending in 00 that is not a leap year.
7. A year ending in 00 that is a leap year.
8. An even-numbered year that is in fact a leap year.
9. A string that has a legitimate year followed by a comment.

For each of the above tests, embed a test string in your program. Run your program.

- Second, implement the file-handling part of this program using try-except suites. Remove the test strings from your program and place them in a “test.txt” file. Implement your try-except group and read and process the “test.txt” input file and write the leap years to a results file. You should get the same results as before. Open the output file created by the above using Notepad and copy it into the “Test(s)” section of your Program Assignment Submission Sheet.
- Third, obtain the text file “all_years.txt” from Blackboard and submit it to your program for processing. Print the resultant output file from Notepad and submit it with this assignment along with your source code for only the final version of your program.

Here's what's on the "all_years.txt":

"Past
2001
2002
2010
1900
1904 World's Fair
19ol
1946
1984
2000

#George Washington's birth year
1732
1855
2012
Future
2018
2022
2025 will be here soon
2050
20456
2076
2300

Century Year
2400
9996
1584
2020
22222
1592
1765
l9ll
2ll0
ABCD
1866
1800
2404
3.14
1948
1962

Present
2016
lolo
1944
4848
5000
3100
2240
2241
8904
1950
1848
1607 Halley's comet?
1620

Lemming
2500
xyzw
2100
2101
2102
love
hate
3232
2054
2.71828
6504
6500
4242
1522
0.68
3333
666
325
1066
6464"

I know this looks like a lot, but I believe most of it is just supposed to be helpful directions. This is due on 09/19/2017, so I have a little bit of time to work on it, but I just need help understanding the actual program first and creating an algorithm. So like the description says, I'll need the program to read the "all_years.txt" file line by line, and output to a separate file all of the years that are leap years, right? But I don't understand the part about asking the user to input file names or how a try-except construct works when opening a file.

Sorry this is such a long post, it's just all of the information that my teacher gave me, so I assume all of it was necessary. If you just look at the description, maybe that would be enough for you guys to help me come up with a program structure or algorithm. He said to use at least 2 functions. I know one of them is to calculate what years are leap years, but what about the other one?

I appreciate any help I can get, cause I have a Calculus II exam that I have to study for over the weekend and I'm super stressed about all of the other work I need to get done, especially this program, even though it's probably simple.

Thanks!
Reply
#2
Well, as an intermediate java programmer what do you understand by 'user input'? seriously...
it's not possible to say it more clear -
Quote:Ask the user to input the file names of both files, and use the try-except construct when you open each in your program
both files being input file and output file. Regarding try-except - you need to handle situations when user input is incorrect like non-existing file...
Reply
#3
To get input from the user, use the input function:

read_name = input('Please enter the name of the file to read: ')
I'm not sure exactly what he means by try/except for a file, but I assume he means something like this:

try:
    read_file = open(read_name)
except IOError:
    # give a warning to the user and quit the program.
It may be something he went over in class, I'd check any materials he's handed out.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Sep-13-2017, 08:20 PM)buran Wrote: Well, as an intermediate java programmer what do you understand by 'user input'? seriously...
it's not possible to say it more clear -
Quote:Ask the user to input the file names of both files, and use the try-except construct when you open each in your program
both files being input file and output file. Regarding try-except - you need to handle situations when user input is incorrect like non-existing file...

I guess I may have worded that wrong. I know what user input is, but what I meant was, I don't know how it's being used with the try-except construct which I've never used before. Also, we just finished learning basic syntax and commands and have done a couple of basic programs... Now all of a sudden, the next program involves reading/writing files, and well as functions which we've never even used before, and has several pages of instructions opposed to a short description. I guess I'm just really trying to understand the program before I start diving into it.

Also, do you think you could help me come up with a structure for this assignment?

(Sep-13-2017, 08:22 PM)ichabod801 Wrote: To get input from the user, use the input function:

read_name = input('Please enter the name of the file to read: ')
I'm not sure exactly what he means by try/except for a file, but I assume he means something like this:

try:
    read_file = open(read_name)
except IOError:
    # give a warning to the user and quit the program.
It may be something he went over in class, I'd check any materials he's handed out.

Ok, thank you. I'll do my best to figure it out, but do you mind helping come up with a structure or algorithm for the program? It'd help me out a lot.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020