Question

Intro to python: Billy is writing a small script to save names he enters in to...

Intro to python:

Billy is writing a small script to save names he enters in to a file using a specified separator. He reads in names until the user enters an exclamation point indicating the end of the list of names at which point it writes the list to a file. Billy, however, is getting a strange error and isn't sure about how to fix the issue. Describe what is the error and how can you fix it?

def write_list_to_file(file_path, lst, sep='\n'):
   assert(isinstance(file_path, str)), "The file_path parameter must be a string."
   assert(isinstance(lst, list)), "The lst parameter must be a list."
   assert(isinstance(sep, str)), "The sep parameter must be a string."
file_h = open(file_path, "a")
file_h.write(sep.join(lst))
if sep != '\n':
file_h.write('\n')
file_h.close()

list_of_names = []

output_file = input("Output File: ")

separator = input("Separator: ")
if separator == "newline":
separator = '\n'
elif separator == "tab":
separator = '\t'
  
while True:
name = input("Name: ")
if name == '!':
break
else:
list_of_names.append(name)

write_list_to_file(output_file, list_of_names, separator)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution

Please find the snippet of code below..

def write_list_to_file(file_path, lst, sep='\n'):

assert(isinstance(file_path, str)), "The file_path parameter must be a string."

assert(isinstance(lst, list)), "The lst parameter must be a list."

assert(isinstance(sep, str)), "The sep parameter must be a string."

file_h = open(file_path, "a")

file_h.write(sep.join(lst))

if sep != '\n':

file_h.write('\n')

file_h.close()

list_of_names = []

output_file = input("Output File: ")

separator = input("Separator: ")

if separator == "newline":

separator = '\n'

elif separator == "tab":

separator = '\t'

while True:

name = input("Name: ")

if name == '!':

break

else:

list_of_names.append(name)

write_list_to_file(output_file, list_of_names, separator)

OUTPUT

Alright Dude, If that worked for you... don't forget to give THUMBS UP.(that will work for me!)

Add a comment
Know the answer?
Add Answer to:
Intro to python: Billy is writing a small script to save names he enters in to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (c programming): write a program that contains a function named getName, that does not get any...

    (c programming): write a program that contains a function named getName, that does not get any parameter and returns a character array of a random name from a constant list of 30 names, in a global array. the function returns that random name. each name in the list is a character string that consists of not more than 20 characters(not including finishing 0). the name consists of only characters from the american alphabet (a-zA-Z) and does not contain any "white"...

  • PYTHON The provided code in the ATM program is incomplete. Complete the run method of the...

    PYTHON The provided code in the ATM program is incomplete. Complete the run method of the ATM class. The program should display a message that the police will be called after a user has had three successive failures. The program should also shut down the bank when this happens. [comment]: <> (The ATM program allows a user an indefinite number of attempts to log in. Fix the program so that it displays a message that the police will be called...

  • Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names,...

    Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names, and read in course names from an input file. Then do the output to show each course name that was read in. See Chapter 8 section on "C-Strings", and the section "Arrays of Strings and C-strings", which gives an example related to this lab. Declare the array for course names as a 2-D char array: char courseNames[10] [ 50]; -each row will be a...

  • Something is preventing this python code from running properly. Can you please go through it and...

    Something is preventing this python code from running properly. Can you please go through it and improve it so it can work. The specifications are below the code. Thanks list1=[] list2=[] def add_player(): d={} name=input("Enter name of the player:") d["name"]=name position=input ("Enter a position:") if position in Pos: d["position"]=position at_bats=int(input("Enter AB:")) d["at_bats"] = at_bats hits= int(input("Enter H:")) d["hits"] = hits d["AVG"]= hits/at_bats list1.append(d) def display(): if len(list1)==0: print("{:15} {:8} {:8} {:8} {:8}".format("Player", "Pos", "AB", "H", "AVG")) print("ORIGINAL TEAM") for x...

  • Create a python script to manage a user list that can be modified and saved to...

    Create a python script to manage a user list that can be modified and saved to a text file. Input text file consisting of pairs of usernames and passwords, separated by a colon (:) without any spaces User choice: (‘n’-new user account, ‘e’-edit existing user account, ‘d’- delete existing user account, ‘l’- list user accounts, ‘q’-quit) List of user accounts, error messages when appropriate Output text file consisting of pairs of username and passwords, separated by a colon (:) without...

  • Chapter 8 Python Case study Baseball Team Manager For this case study, you’ll use the programming...

    Chapter 8 Python Case study Baseball Team Manager For this case study, you’ll use the programming skills that you learn in Murach’s Python Programming to develop a program that helps a person manage a baseball team. This program stores the data for each player on the team, and it also lets the manager set a starting lineup for each game. After you read chapter 2, you can develop a simple program that calculates the batting average for a player. Then,...

  • Could someone please help me write this in Python? If time allows, it you could include...

    Could someone please help me write this in Python? If time allows, it you could include comments for your logic that would be of great help. This problem involves writing a program to analyze historical win-loss data for a single season of Division I NCAA women's basketball teams and compute from this the win ratio for each team as well as the conference(s) with the highest average win ratio. Background Whether it's football, basketball, lacrosse, or any other number of...

  • Python Coding The script, `eparser.py`, should: 1. Open the file specified as the first argument to...

    Python Coding The script, `eparser.py`, should: 1. Open the file specified as the first argument to the script (see below) 2. Read the file one line at a time (i.e., for line in file--each line will be a separate email address) 3. Print (to the screen) a line that "interprets" the email address as described below 4. When finished, display the total number of email addresses and a count unique domain names Each email address will either be in the...

  • PYTHON PROGRAMMING LANGUAGE (NEEDED ASAP) Using a structured approach to writing the program: This section will...

    PYTHON PROGRAMMING LANGUAGE (NEEDED ASAP) Using a structured approach to writing the program: This section will guide you in starting the program in a methodical way. The program will display a window with GUI widgets that are associated with particular functions: Employee Payroll O X -- - - - - - - - - - - Show Payroll Find Employee by Name Highest Lowest Find Employee by Amount Write Output to Fie Cancel - -------- ------------- Notice that some of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT