Tuesday, 14 April 2015

Course Work Practice #3


For this piece of coursework practice, we were asked to create a program that asks students in a class how much money they spent on 1) Clothes: , 2) CDs: , and 3) Food:. The students have a budget for 2000dhs to spend. 
The best way to store this data would be to use a list, and so at the beginning, we create 3 empty lists of the items that they would spend their money on:




For this program, it would be best to use a "for" loop since it executes the same command for as many times as you like. Here, we're only going to ask three students, so the command would be executed only three times.
(note that anything indented in line with the loop becomes part of it and therefore is executed three times)



The next step would be to make sure that each answer is appointed to the correct list in order to avoid confusion, and so that everything is in its right place. To do this, we would need the input() function to take the students' answers, and assign them to a variable, while converting the input into an integer using the int() method.
for example:






Here its shown that the input for "x" stores the input for "Clothes:" and same goes for the variables "y" and "z".

Then, we would need to .append() the variables to the three lists assigned to the three items money was spent on, like so:






 The variables that are appended to the three lists contain each list's input.

We would then need to create a variable that store the sum of each list's contents, and the sum() would be used. 





So, the "clothes = []" variable now storing the students' input regarding the money spent on clothes.






Now, we insert the sum of each lists' contents using .insert(index, variable), "0" being the index, and "sumclothes" being the variable.
All thats left now is to print our lists:

Then we would need to see which item had the most money spent on it using:

These statements basically mean "if sum of clothes is greater than the sum of cd's and if sum of clothes is greater than the sum of food, print the sum of clothes and print("The most money was spent on clothes.").
And so on for the other lists.


The final result should be:















And then you'd be left with a fully functioning "Shopping list".

Sunday, 12 April 2015

Course Work Practice Task #2

For this task, it requires the same methods and structure, so I do not see the need to elaborate further on either.
The final result should be:

Course Work Practice

In class, we were handed a sheet with a bunch of tasks that are to be completed as a practice for our upcoming coursework. For one of the tasks, we needed to create a program that has girl shoe sizes in a list, and the program would need to add the sum of the shoe sizes, and add the total to the beginning of the list, and then create another list that counts how many of the shoe sizes there are. 
Then we need to create the same code for boy shoe sizes, and in the end compare the sums of each list.

So, in the beginning, we would need to create an empty list called "girl_shoes", and on a separate line, we add the contents of the list using the method ".extend()". Since we need the sum of the shoe sizes added to the beginning of the list, we create a variable, "total_girls" that holds the value of the sum of the shoes, and use the "sum" method to add the contents of our list (considering they are integers)  together. For this part, I get slightly confused as its an unfamiliar approach, but its a simple one. To actually add the sum to the beginning of the "girl_shoes" list, we use the ".insert()" method. As seen in the code, I wrote it like this : 
"girl_shoes.insert (0,total_girls)". Now, its clear that "girl_shoes" is the name of the list that the ".insert()" method is being applied to, however, in the parenthesis, you need to mention the index that you want your content to be added at, and to what variable or list. So in this case, I wrote : "(0,total_girls)", "0" being the index I want it to be inserted at, and "total_girls" the variable that referred to the list I wanted to add it to.
And now we add the sum to our list, and print it.
The next step would be to count how many of the shoe sizes there were. To do this, we would need to use the ".count()". However since it was needed for every shoe size, we would need to create a variable for each shoe size, and then apply the ".count()" method. Then all that's left is to call on each method for each shoe size, and compare the sum of the contents of each list using the following if and else statements as seen below:
The final result should be:


Hope this could be of use later on.  :3