The assignments will be graded out of 100 points. Create a text document entitled answers.xxx (where you replace xxx with whatever extension is appropriate, depending on the file format you use). Acceptable file formats are plain text, Word document, OpenOffice document, and PDF. Put your name and UTA ID in the file on the first line. Your answers that are not part of a program will be added to this file. Each task below will instruct you where to put your answers.
The following code is supposed to create a variable list1
that stores list2
in reverse order. It contains an error and will not run. In answers.xxx, explain
what is wrong. Correct the code and save it as task1.py. Do not use the reverse() list
method in your answer.
list1 = [] list2 = ['a', 'b', 'c', 'd'] number = len(list2)-1 while number >= 0: append(list1, list2[number]) number = number - 1 for item in list1: print(item)
The following code is supposed to print elements from position 0 up to and including position 3 of my_list
, but does not work correctly.
In your answers.xxx file, explain what is wrong. Correct the code and save it as task2.py.
my_list = ['bob', 'jane', 'mary', 'tom', 'rick'] for name in my_list[0:3]: print(name)
Mr. Bugs, in one of his programs, attempts to make a copy (called L2
) of list L1
so that
he can experiment with the copy and not accidentally modify the original version. Whenever he tries to use pop() on
the copy, it keeps modifying the original. In your answers.xxx file, explain what is wrong. Correct the code and save it as task3.py.
L1 = [1, 2, 3] L2 = L1 item = L2.pop() print(item) print(L1)Note: the above code produces the following output:
3 [1,2]
Write a program that asks the user for 5 integers, stores them in a list and prints the numbers in the list in order from largest to smallest. Save your program as task4.py.
Write a program that asks the user for 5 strings, stores them in a list, swaps the first and last word in the list, and prints the words at index positions 2 through the end. Use slicing to obtain the words at position 2 through the end in order to print them. Save your program as task5.py.
1 5 6 7 0 10 3 -5 33
A mathematical matrix, like the one above, can be simulated with nested lists, in which each row of the matrix is represented as a list. The row lists are then stored in a master list, resulting in the following:
L = [[1, 5, 6], [7, 0, 10], [3, -5, 33]]
The elements can be accessed in this manner: L[row_number][column_number], so to access the 33 in the above example, you would use L[2][2].
Write a program that:
1, 0, 33The exact output format is not important (whether there are commas or not between the items, or whether each item is on a different line or all items are on the same line).
When the user enters the numbers, the top row should be entered first (from left to right), then the middle row, and, finally, the bottom row. The numbers for our example would be entered in this order: 1, 5, 6, 7, 0, 10, 3, -5, 33. Note that this will require the user to be prompted for an integer 9 times.
Save your program as task6.py.
In this task, you will create a simple food inventory system, that keeps track of different food types and the quantity available for each food type. Create a loop that continually asks the user for a food and a quantity and stores them in a single
list of the form foods = [food1, quantity1, food2, quantity2, food3, quantity3]
. If the user enters the
letter 'q' or 'Q', stop the loop and print the inventory in the following manner:
food1 : quantity1 food2 : quantity2 food3 : quantity3
The exact format matters in this task. For full credit, your program should follow the exact same format.
Save your program as task7.py.
The assignment should be submitted via Blackboard. Submit a ZIPPED directory called assignment3.zip (no other forms of compression accepted, contact the instructor or TA if you do not know how to produce .zip files). The zipped directory should contain your answers.xxx document and all the Python code files (task5.py, task6.py, etc).