Question:
Python Problem!?
jack
2015-11-15 15:37:31 UTC
1-Create a list of five names, such as ['Margot', 'Kathryn', ‘Pamela’]. Create a for loop that iterates through the list and creates a string with the words “Hi’, the name of the friend and the words ". Please come to my party on Saturday!" so that the result looks like:

Hi Margot. Please come to my party on Saturday!
Hi Kathryn. Please come to my party on Saturday!
Hi Pamela. Please come to my party on Saturday!

2-Create two lists: list1 = [2, 6, 10] and list2 = [1, 3, 5]. Create a for loop that iterates through the two lists and calculates the difference between the respective element of each list. Also in the for loop, find the difference squared and keep a running total of that number.

Print the difference after each time it is calculated and the sum after each time it is calculated, so that it looks like this:

The difference is xxx

The sum is yyy

where xxx and yyy are the calculated values.

3-Write a program that generates all odd numbers from 1 to n. Ask the user for n and set n in the beginning of the program. Use a while loop to compute the numbers and create a list that contains the numbers. The print out the resulting list.
Three answers:
?
2015-11-18 16:20:45 UTC
2-Create two lists: list1 = [2, 6, 10] and list2 = [1, 3, 5]. Create a for loop that iterates through the two lists and calculates the difference between the respective element of each list. Also in the for loop, find the difference squared and keep a running total of that number.



Well, I assume both list parameters will always have the same length and, consequentially, the same number of indices to iterate over.



def foo(list1,list2):

difference_squared = 0

for n in range(len(list1)):

difference = (list2[n]-list1[n])

difference_squared = difference_squared + difference**2

return difference_squared
joe
2015-11-15 15:50:27 UTC
It's homework, let us see you work thus far, and then explain what problems you are having. It is not our responsibility to do you homework for you but for yours so you can learn.
2015-11-15 19:54:02 UTC
for bìtch in ['Margot', 'Kathryn', 'Pamela', 'EddieJ']:

  print 'Hi, ' + bìtch + '. Please come to my orgy on Saturday!'


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...