The New Jim Crow
July 4, 2020
Nurse leader
July 4, 2020
Show all

Beginner’s Python

Part 1:

Write code that writes the names of three of your favorite movies to a file named movies.txt.

Each movie should be on a separate line in the text file.

Use file explorer to manually open the text file in a text editor (like Notepad, Notepad++, etc.) to verify your Python code wrote three movie names, one per line.
Sample output to a file:

The Matrix
Gladiator
Top Gun

Part 2:

Write a program that asks the user for the name of a file.

The program should display only the first five lines of the files contents if the file contains five or more lines.

If the file contains less than five lines, it should display the files entire contents.

Sample input/output (5 or more lines in the file):
Enter the name of the file: pokemon.txt

Bulbasaur
Ivysaur
Venusaur
Charmander
Charmeleon

Sample input/output (less than 5 lines in the file):
Enter the name of the file: pokemon.txt

Bulbasaur
Ivysaur
Venusaur

Part 3:
Use the movies.txt file you created in Lab 12.

Write code that opens the movies.txt file and appends three new movies to the end of the file. The three new movies must not overwrite the existing three movies already in your file.

Use file explorer to manually open the text file in a text editor (like Notepad, Notepad++, etc.) to verify six movie names were written, one per line.
Sample output to a file:

The Matrix
Gladiator
Top Gun
Shawshank Redemption
Second Hand Lions
Back to the Future

Part 4:

Download the scores.txt file. (https://elearn.volstate.edu/content/enforced/8012611-16023.202010/scores.txt?_&d2lSessionVal=mxCQ12wFgLrnN0ayxsA6n2leF) It contains a series of names and scores (an integer between 1 and 100).

Place the scores.txt file in the same folder as your assignment 13 Python file.

Open scores.txt and review the format and layout of the file.

Write a program that reads scores.txt and displays the name of the student with highest score and the actual score (see output below).

Also print the the number of records in the file.

TIP #1: You can use an if statement to keep track of the highest score found (and the name of the student the score belongs to) as you read through the records, and a variable to keep count of the number of records.

TIP #2: If you’re not sure where to begin, read section 6.3, “Processing Records”.

Sample output:
Highest Score: Michael, 97
Number of records: 12

Part 5:

Perform the following steps in the file you created above (lastname_firstname_lab14.py):

Write a function called is_valid_email(email_address) that validates an email address.

The email address to validate is: [email protected]
Iterate over the string looking for the @ character.

Verify the string ends with: .edu

If both the @ character and .edu is found, then the email address is valid.

The function should return true if the email is valid. The function should return false if the email is invalid.
Call the is_valid_email function from your main code and print the result (true or false) returned by the function.

Sample output (for a valid email address):

true
Sample output (for an invalid email address):

false

Part 6:

Write a program that accepts a sentence as input and converts each word to Pig Latin.

In this version of Pig Latin: to convert a word to Pig Latin, you remove the first letter and place that letter at the end of the word. Then, you append the string ay to the word. See below for example input and output.

Make a function called english_to_piglatin(input), where the function accepts a string (entered by the user in #1 above) of what to convert to Pig Latin.

Call the english_to_piglatin function from your main code.

Optional Challenge: Create the reverse function: piglatin_to_english. This function coverts Pig Latin back to English.
TIP for #5: Suppose a sample string is named “word” and its value is set to “something”. word[0] slices the “s”.  word[9] slices the “g”. word [7:9] slices”ng”. You can see how this is helpful for reversing Pig Latin. However, since the string could be any number of characters, you cannot hard-code 7 and 9. You must use the string length as a relative basis.

Sample input/output:
English input: I SLEPT MOST OF THE NIGHT
Pig Latin output: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY

Sample input/output (from Optional Challenge #5)
English input: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY
Pig Latin output: I SLEPT MOST OF THE NIGHT

Leave a Reply

Your email address will not be published. Required fields are marked *