[codecademy] 6. Student Becomes the Teacher

Posted by 알 수 없는 사용자
2015. 11. 7. 21:21 프로그래밍/Python

Student Becomes the Teacher

Student Becomes the Teacher 1/9

Lesson Number One

챌린지 코스 1번째!!

참고코드

문제

1. Create three dictionaries: lloyd, alice, and tyler.

2. Give each dictionary the keys "name", "homework", "quizzes", and "tests".

3. Have the "name" key be the name of the student (that is, lloyd's name should be "Lloyd") and the other keys should be an empty list. (We'll fill in these lists soon!)

Student Becomes the Teacher 2/9

What's the Score?

잘했어요!

문제

Now fill out your lloyd dictionary with the appropriate scores. To save you some time, we've filled out the rest for you.


Homework: 90.0, 97.0, 75.0, 92.0

Quizzes: 88.0, 40.0, 94.0

Test Scores: 75.0, 90.0


Make sure to include the decimal points so your grades are stored as floats! This will be important later.

Student Becomes the Teacher 3/9

Put It Together

세개의 딕셔너리를 하나의 리스트로 모아보자.

참고 코드

문제

Below your code, create a list called students that contains lloyd, alice, and tyler.

Student Becomes the Teacher 4/9

For the Record

잘했어요!

참고 코드

문제

for each student in your students list, print out that student's data, as follows:


1. print the student's name

2. print the student's homework

3. print the student's quizzes

4. print the student's tests

Student Becomes the Teacher 5/9

It's Okay to be Average

설명없음!

참고 코드

문제

Write a function average that takes a list of numbers and returns the average.


1. Define a function called average that has one argument, numbers.

2. Inside that function, call the built-in sum() function with the numbers list as a parameter. Store the result in a variable called total.

3. Like the example above, use float() to convert total and store the result in total.

4. Divide total by the length of the numbers list. Use the built-in len() function to calculate that.

5. Return that result.

Student Becomes the Teacher 6/9

Just Weight and See

참고코드

문제

Write a function called get_average that takes a student dictionary (like lloyd, alice, or tyler) as input and returns his/her weighted average.


1. Define a function called get_average that takes one argument called student.

2. Make a variable homework that stores the average() of student["homework"].

3. Repeat step 2 for "quizzes" and "tests".

4. Multiply the 3 averages by their weights and return the sum of those three. Homework is 10%, quizzes are 30% and tests are 60%.

Student Becomes the Teacher 7/9

Sending a Letter

잘했어요!

문제

1. Define a new function called get_letter_grade that has one argument called score. Expect score to be a number.

2. Inside your function, test score using a chain of if: / elif: / else: statements, like so:

If score is 90 or above: return "A"

Else if score is 80 or above: return "B"

Else if score is 70 or above: return "C"

Else if score is 60 or above: return "D"

Otherwise: return "F"

3. Finally, test your function! Call your get_letter_grade function with the result of get_average(lloyd). Print the resulting letter grade.

Student Becomes the Teacher 8/9

Part of the Whole

Good! 이제 반평균을 계산해보자

1. Define a function called get_class_average that has one argument students. You can expect students to be a list containing your three students.

2. First, make an empty list called results.

3. For each student item in the class list, calculate get_average(student) and then call results.append() with that result.

4. Finally, return the result of calling average() with results.

Student Becomes the Teacher 9/9

How is Everybody Doing?

잘했어요!

문제

Finally, print out the result of calling get_class_average with your students list. Your students should be [lloyd, alice, tyler].

Then, print the result of get_letter_grade for the class's average.


'프로그래밍 > Python' 카테고리의 다른 글

[codecademy] 7.LISTS AND FUNCTIONS  (317) 2015.11.07
[codecademy] 4. FUNCTIONS  (314) 2015.09.05
[codecademy] 5.LISTS & DICTIONARIES  (319) 2015.08.28
[codecademy] 3.CONDITIONALS AND CONTROL FLOW  (301) 2015.08.10
Python 시리얼 출력(serial 통신)  (4) 2015.07.24

[codecademy] 7.LISTS AND FUNCTIONS

Posted by 알 수 없는 사용자
2015. 11. 7. 20:44 프로그래밍/Python

LISTS AND FUNCTIONS

Lists and Functions 1/18

List accessing

리스트의 정보를 가져오고 바꿀수있다.

문제

Please add the code to print out the second element in the list.

Lists and Functions 2/18

List element modification

리스트의 값을 바꾸는법을 알아보자.(복습)

문제

On line 3, multiply the second element of the n list by 5

Overwrite the second element with that result.

Make sure to print the list when you are done!

Lists and Functions 3/18

Appending to a list

.append() 함수를 이용하여 리스트의 마지막 부분에 값을 추가할수 있다.

문제

Append the number 4 to the end of the list n.

Lists and Functions 4/18

Removing elements from lists

리스트에서 값을 삭제시키는 방법은 3가지가 있다.

1. n.pop(index) : 해당인덱스에 있는 값을 제거하고 그값을 리턴한다.

2. n.remove(item) : 리스트에서 item에 해당하는 값을 삭제한다.( 매개변수가 인덱스가 아닌것에 주의 )

3. del(n[1]) : .pop와 같이 인덱스 값으로 삭제하지만 리턴값이 없다.

문제

Remove the first item from the list n using either .pop(), .remove(), or del.

Lists and Functions 5/18

Changing the functionality of a function

1~4번 문제들의 내용을 함수안에서 구현할 수 있다.

문제

Change the function so the given argument is multiplied by 3 and returned.

Lists and Functions 6/18

More than one argument

하나 이상의 매개변수를 갖는 함수를 복습해보자

문제

Define a function called add_function that has 2 parameters x and y and adds them together.

Lists and Functions 7/18

Strings in functions

문자열을 매개변수로 하는 함수를 복습해보자

문제

Write a function called string_function that takes in a string argument (s) and then returns that argument concatenated with the word 'world'.

Don't add a space before world!

Lists and Functions 8/18

Passing a list to a function

함수의 매개변수로 여러가지 변수형을 사용할수 있고, 리스트도 같은 방법으로 함수의 매개변수로 쓸 수 있다.

문제없음

Lists and Functions 9/18

Using an element from a list in a function

함수의 매개변수로 리스트를 사용하는 방법이다.

문제

Change line 2 so that list_function returns only the item stored in index one of x, rather than the entire x list.

Lists and Functions 10/18

Modifying an element of a list in a function

함수안에서 리스트의 값을 바꾸는것도 함수 밖에서 리스트의 값을 바꾸는방법이랑 같다.

문제

Change list_function so that:

Add 3 to the item at index one of the list.

Store the result back into index one.

Return the list.

Lists and Functions 11/18

List manipulation in functions

리스트에 값을 추가하는 append()도 함수안에서 사용할수 있다.

문제

1. Define a function called list_extender that has one parameter lst.

2. Inside the function, append the number 9 to lst.

3. Then return the modified list.

Lists and Functions 12/18

Printing out a list item by item in a function

renge()함수를 이용하여 함수안에서 리스트의 값들을 출력할수 있다 

문제

1. Define a function called print_list that has one argument called x.

2. Inside that function, print out each element one by one. Use the existing code as a scaffold.

3. Then call your function with the argument n.

Lists and Functions 13/18

Modifying each element in a list in a function

리스트의 요소들을 각각 변경할수 있다. len()은 리스트의 길이를 반환한다.

문제

Create a function called double_list that takes a single argument x (which will be a list) and multiplies each element by 2 and returns that list.

Use the existing code as a scaffold.

Lists and Functions 14/18

Passing a range into a function

range()함수는 해당범위를 리스트로 만들어 준다. 

range(start,stop,step)

start 부터 시작해서 stop 까지 step 만큼 건너뛰며 리스트로 만든다. 

문제

On line 6, replace the ____ with a range() that returns a list containing [0, 1, 2].


Lists and Functions 15/18

Iterating over a list in a function

리스트를 통해 두가지 방법으로 반복문을 사용할수 있다.

1. for item in list:


2. 인덱스를 통한 반복문


2번째 방법은 리스트의 값을 일일히 변경할수 있다.

문제

Create a function that returns the sum of a list of numbers.

1. On line 3, define a function called total that accepts one argument called numbers. It will be a list.
2. Inside the function, create a variable called result and set it to zero.
3. Using one of the two methods above, iterate through the numbers list.
4. For each number, add it to result.
5. Finally, return result.

Create a function called total that adds up all the elements of an arbitrary list and returns that count, using the existing code as a hint. Use a for loop so it can be used for any size list.



Lists and Functions 16/18
Using strings in lists in functions

15번에서 배운 방법을 문자열에도 적용시켜보자.

문제

Create a function that concatenates strings.

1. Define a function called join_strings accepts an argument called words. It will be a list.
2. Inside the function, create a variable called result and set it to "", an empty string.
3. Iterate through the words list and append each word to result.
4. Finally, return the result.

Don't add spaces between the joined strings!

Lists and Functions 17/18

Using two lists as two arguments in a function

리스트 두개를 매개변수로 받아서 합칠수 있다.


문제
Create a function that joins two lists together.

1. On line 4, define a function called join_lists that has two arguments, x and y. They will both be lists.
2. Inside that function, return the result of concatenating x and y together.


Lists and Functions 18/18

Using a list of lists in a function

마지막으로 하나의 리스트에 여러개의 리스트가 들어가있을때 함수에서 사용하는 방법이다.

 


문제
Create a function called flatten that takes a single list and concatenates all the sublists that are part of it into a single list.

1. On line 3, define a function called flatten with one argument called lists.
2. Make a new, empty list called results.
3. Iterate through lists. Call the looping variable numbers.
4. Iterate through numbers.
5. For each number, .append() it to results.
6. Finally, return results from your function.



NOSQL

Posted by 알 수 없는 사용자
2015. 11. 6. 00:23 프로그래밍/데이터베이스