Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ASSIGNMENT Lab 10 - File I/O and Looping 1) You should have a file calledname_list.txt (see below). This is a list of unique randomly generated

ASSIGNMENT Lab 10 - File I/O and Looping

1) You should have a file calledname_list.txt (see below). This is a list of unique randomly generated names based on popularity of given names and surnames in the United States in the format "first last". Write some code to create a new file with the same names, one per line, but in the format "last, first". but use looping in some fashion.

a. Open the file for reading

i. suggestion: use variable nameinfile

b. Open a new file for writing.

i. suggestion: use variable nameoutfileand file namenew_names.txt

c. Read in one line at a time from infile

i. Split each line on spaces

ii. Using index notation, rearrange order of names

iii. Write tooutfile(don't forget the line return!)

** For bonus points, alphabetize your names by last name

2) Which line (starting with the first line as line #1 as it appears in Notepad++) in name_list.txt

contains the name "Mason Howard"? Write a python script to figure this out (don't just open it

in Notepad++ and search for the name). Below is one way to do this:

a. Open the file for reading

b. Read in one line

c. Start a counter at 1

d. WHILE that line does not equal "Mason Howard", read in the next line, increment the

counter.

e. When it does equal "Mason Howard", print the counter value.

** For bonus points, write a function that takes the name as an input, so you can find any

person's line number, assuming they are in the file. The filename can be hard-coded in the

function.

NAME LIST

Mateo Scott

Liam Bennett

Alejandro Reyes

Daniel Gutierrez

Nikhil Powell

Dylan Gray

Samuel Martin

Alexander Stewart

Matthew Garcia

David Gomez

Matthew Davis

Liam Wilson

Alejandro Hill

Santiago Cook

Deven Wilson

William Murphy

Mateo Bell

Carter Allen

Jacob Stewart

Diego Perry

Daniel Jenkins

Mason Hernandez

Jack Brown

Deven Howard

Liam James

Joseph Gomez

Noah Allen

Jacob Gutierrez

Mason Howard

Jason Thomas

James James

Liam Baker

Alexander Fisher

John Wilson

Eric Cook

Jacob Diaz

William Thomas

Liam Perez

Ethan Butler

Noah Hill

Joshua Thomas

Jayden Jackson

Liam Moore

John Allen

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions