Question

In: Computer Science

PROBLEM 5: Code for the Ages Function Name: goldenAges() The coding language is Jes Python Parameters:...

PROBLEM 5: Code for the Ages Function Name: goldenAges()

The coding language is Jes Python

Parameters: ● list1: a list containing the names of the TAs ● list2: a list containing the ages of the TAs

Return Value: none

Description: I have to concede defeat. It's pretty hard to be creative and reference memes when working with File I/O, so you guys will get to learn something about us you might not already know: our ages!! For this problem, write a function that takes in two lists and writes to a file called "TAages.txt". The function should match the ages to the correct TA and write sentences which say how old each TA is on separate lines. The TA names should be written to the file in reverse order, and the ages written in the same order as your parameter. Remember to close your file at the end!

Test Cases: >>>goldenAges(["Genevieve","Tanya","Khalil","Ryan","Christina"],[19,21,22,21,19])

Solutions

Expert Solution

Code:

Code as text:

# function to take list and write to files
def goldenAges(TAnames, TAages):
f = open("TAages.txt", 'w') #open file
list_length = len(TAnames) #get length of lists
for i in range(list_length):
    r = list_length - i - 1 # index from end
    f.write("TA %s is %d years old.\n" % (TAnames[r], TAages[i])) #write to file

f.close() #close file


# test
goldenAges(["Genevieve", "Tanya", "Khalil", "Ryan", "Christina"], [19,21,22,21,19])

Sample run Output file:

P.s. Please ask any doubts in comments and rate the answer


Related Solutions

Using python as the coding language please write the code for the following problem. Write a...
Using python as the coding language please write the code for the following problem. Write a function called provenance that takes two string arguments and returns another string depending on the values of the arguments according to the table below. This function is based on the geologic practice of determining the distance of a sedimentary rock from the source of its component grains by grain size and smoothness. First Argument Value Second Argument Value Return Value "coarse" "rounded" "intermediate" "coarse"...
Language: Python Function name : sort_by_rating Parameters : file (.csv file), category (string), order (boolean) Returns:...
Language: Python Function name : sort_by_rating Parameters : file (.csv file), category (string), order (boolean) Returns: None Description : You want to see what order the items of a particular category in your file would be in if you sorted them by rating. Given a file, a category of clothing, and an order boolean (True for ascending order, False for descending order), create a function that writes a file called “sorted_items.csv” that includes all of the items in the specified...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
Python Coding Create function openAndReturnLastN() to meet the conditions below - accept 2 parameters; 1 string...
Python Coding Create function openAndReturnLastN() to meet the conditions below - accept 2 parameters; 1 string (name of a file) and 1 integer (N) - add a docstring - use exception handling techniques to attempt to open the filename provided, if it does not exist, return False - the file will contain an unknown number of lines - store the last N lines of the file in a single string var - return the resultant string var --- ensure to...
Using Python coding language (with or without Pandas and/or NumPy), 1. Can you define function sleep...
Using Python coding language (with or without Pandas and/or NumPy), 1. Can you define function sleep to tell whether the participant are of the ages through 18 to 60 and sleep less than 6 hours per day? 2. Develop codes to check whether the sleep function you defined can make correct judgement. Make sure you write comments or create informative vairable name so that I can understand how you check the sleep function. (Hints: You can create toy data/dataframe to...
Code in Python 1. A function named get_score has been defined which consumes no parameters and...
Code in Python 1. A function named get_score has been defined which consumes no parameters and returns an int value. Call get_score and print the result. 2.A function named area_rectangle has been defined which consumes two parameters that are both float values and returns a float value. Call area_rectangle with arguments of variables named length and width and print the result. 3. Assume a function called calculate_cone_volume is already defined. The function has 2 parameters: height and radius (in that...
Question 5 (10 marks) Python Language What is the output of the following code ? (2...
Question 5 Python Language What is the output of the following code ? (2 points) a, b = 0, 1 while b < 10: print b a, b = b, a+b B. Explain List Comprehension (2 points) Given v = [1 3 5] w = [ [2*x, x**2] for x in v] What is the content of w? c. What is tuple ?   What is the difference between tuple and list ? (2 points) D. What is a module ?  ...
use the python language and fix the error code #Write a function called rabbit_hole. rabbit_hole should...
use the python language and fix the error code #Write a function called rabbit_hole. rabbit_hole should have #two parameters: a dictionary and a string. The string may be #a key to the dictionary. The value associated with that key, #in turn, may be another key to the dictionary. # #Keep looking up the keys until you reach a key that has no #associated value. Then, return that key. # #For example, imagine if you had the following dictionary. #This one...
Using Python programming language, write a LONG piece of code that utilizes the do while function...
Using Python programming language, write a LONG piece of code that utilizes the do while function and the switch statement, please do not make It short, thank you.
Define a Python function named matches that has two parameters. Both parameters will be lists of...
Define a Python function named matches that has two parameters. Both parameters will be lists of ints. Both lists will have the same length. Your function should use the accumulator pattern to return a newly created list. For each index, check if the lists' entries at that index are equivalent. If the entries are equivalent, append the literal True to your accumulator. Otherwise, append the literal False to your accumulator. Hint: Since you must use the same index with each...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT