Question

Hey all, I am writng some code nad this is what I have so far, I...

Hey all, I am writng some code nad this is what I have so far, I am stuck

-Write a function named "write_list" that takes a list of strings as a parameter and writes the contents of the list to a file named "stranger.txt" with one value per line. If a file named "stranger.txt" already exists it must be overwritten. The function should not return any value

def write_list(lst):

with open ("stranger.txt","w") as file:

for string in lst: file.write(string+'\n')

write_list(lst)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def write_list(lst):
    with open("stranger.txt", "w") as file:
        for string in lst:
            file.write(string + '\n')


# Testing the function here
write_list(["Hi", "How", "are", "you?"])

Add a comment
Know the answer?
Add Answer to:
Hey all, I am writng some code nad this is what I have so far, I...
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
  • python: Write a function named "write_values" that takes a key-value store mapping strings to strings as...

    python: Write a function named "write_values" that takes a key-value store mapping strings to strings as a parameter and writes the values of the input to a file named "persuade.txt" with one element per line. If a file named "persuade.txt" already exists it must be overwritten. The function should not return any value

  • I am having a little trouble with my Python3 code today, I am not sure what...

    I am having a little trouble with my Python3 code today, I am not sure what I am doing wrong. Here are the instructions: and here is my code: update: I have seen I did not close x in sumFile and I am still only getting a 2/10 on the grader. any help appreciated. Lab-8 For today's lab you going to write six functions. Each function will perform reading and/or write to a file Note: In zybooks much like on...

  • I need help in Python. This is a two step problem So I have the code...

    I need help in Python. This is a two step problem So I have the code down, but I am missing some requirements that I am stuck on. Also, I need help verifying the problem is correct.:) 7. Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500. The application should let the user specify how many random numbers the file...

  • Please write this code in python programming and provide a screen short of the code. Reading:...

    Please write this code in python programming and provide a screen short of the code. Reading: P4E 7; Tut 7.2, 7.2.1 Upload an original Python script that satisfies the following criteria: It has at least two functions Function 1: This function has one parameter, a string containing the path to an existing text file that will be read by your function Using a with statement, the function opens the file indicated by the parameter for reading The function counts the...

  • I have the current code that is working. I need to edit it so that I...

    I have the current code that is working. I need to edit it so that I am only counting strings that are after the word 'From' in a text file. Right now it's counting all words, but I just need the From email addresses in a text file. I think the issue if in the if/else loop but nothing I have tried works. Please help! 1 import string 2 fhand open('C: \Users \Brooke\Canopy scripts mbox-short.txt') 3 counts dict( 4 for...

  • Hello, In Python, I am trying to create a function that will return the domain name...

    Hello, In Python, I am trying to create a function that will return the domain name from any inputted email address. For example, my input of [email protected] would return just the domainname. It needs to be done using regular expressions and the map function. I'm still stuck on trying to get the correct parameters for regular expression and also what is throwing it off. Thank you for your help! I am currently working with a list of different email addresses...

  • I am using python3.6 and i am getting an error on line 42 num = int(fin.readline())...

    I am using python3.6 and i am getting an error on line 42 num = int(fin.readline()) #reading first value valueError: invalid literal, for int() with base 10 Any help is appreciated, here is the code. reads from txt file a few integers in an array and sorts them. thank you! # Function to do insertion sort def insertionSort(arr):     # Traverse through 1 to len(arr)     for i in range(1, len(arr)):         key = arr[i]         # Move elements of...

  • Program this in C There is a lot of sorting algorithms. So far, we have covered...

    Program this in C There is a lot of sorting algorithms. So far, we have covered selection and insertion sort. However, we only covered how selection sort is implemented using an array. Every sorting algorithm implemented using an array can also be implemented using a linked list. In this assignment, you will implement the selection sort algorithm using a linked list instead of an array. You will first create an interface for handling string items. Basically, you will need to...

  • in python I have a list of strings, some contain purely numbers while others are dates,...

    in python I have a list of strings, some contain purely numbers while others are dates, names, etc. I am trying to convert the strings containing only numbers into floats. this is the code I am using but it alwyas returns nothing. Python just says restart and nothing goes through. how can I get my code to do what I want? f = open('GOOG-HistoricalQuotest.txt','r+') ff=f.readlines() clean = [] lst = [] for i in range(2,len(ff)+1): try: [float(i) for i in...

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