Chapter 9 (Real Python)

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

9.4 - Write a program that contains the following lists of lists: universities = [ ['California Institute of Technology', 2175, 37704], ['Harvard', 19627, 39849], ['Massachusetts Institute of Technology', 10566, 40732], ['Princeton', 7802, 37000], ['Rice', 5879, 35551], ['Stanford', 19535, 40569], ['Yale', 11701, 40500] ] Define a function, enrollment_stats(), that takes, as an input, a list of lists where each value list contains three elements: (a) the name of a university (b) the total number of enrolled students (c) the annual tuition fees. Enrollment_stats() should return two lists: 1. the first containing all of the student enrollment values 2. and the second containing all of the tuition fees. Next, define a mean() and a median() function. Both functions should take a single list as an argument and return the mean and median of the values in each list. Using universities, enrollment_stats(), mean(), and median(), calculate the total number of students, the total tuition, the mean and median of the number of students, and the mean and median tuition values. Finally, output all values, and format the output so that it looks like this: "****************************** Total students: 77,285 Total tuition: $ 271,905 Student mean: 11,040.71 Student median: 10,566 Tuition mean: $ 38,843.57 Tuition median: $ 39,849 ******************************"

Solution: https://github.com/realpython/python-basics-exercises/blob/master/ch09-lists-tuples-and-dictionaries/4-challenge-list-of-lists.py

9.5- In this challenge, you'll write a program that generates poetry. Create five lists for different word types: Nouns: ["fossil", "horse", "aardvark", "judge", "chef", "mango", "extrovert", "gorilla"] Verbs: ["kicks", "jingles", "bounces", "slurps", "meows", "explodes", "curdles"] Adjective: ["furry", "balding", "incredulous", "fragrant", "exuberant", "glistening"] Preposition: ["against", "after", "into", "beneath", "upon", "for", "in", "like", "over", "within"] Adverbs: ["curiously", "extravagantly", "tantalizingly", "furiously", "sensuously"] Randomly select the following number of elements from each list: 3 nouns 3 verbs 3 adjectives 2 prepositions 1 adverb You can do this with choice() function in te random module. This function takes a list as input and returns a random selected element of the list. For example, here's how you use random.choice() to get random elements from the list ["a", "b", "c"]: import random random_element = random.choice(["a", "b", "c"]) Using the randomly selected words, generate and display a poem with the following structure inspired by Clifford Pickover: {A/An} {adj1} {noun1} {A/An} {adj1} {noun1} {verb1} {prep1} the {adj2} {noun2} {adverb1}, the {noun1} {verb2} the {noun2} {verb3} {prep2} a {adj3} {noun3} Here, adj stands for adjective and prep for preposition. Here's an example of the kind of poem your program might generate: "A furry horse A furry horse curdles within the fragrant mango extravagantly, the horse slurps the mango meows beneath a balding extrovert" Every time your program runs, it should generate a new poem.

Solution: https://github.com/realpython/python-basics-exercises/blob/master/ch09-lists-tuples-and-dictionaries/5-challenge-wax-poetic.py

9.7- Review your state capitals along with dictionaries and while loops! First, finish filling out the following dictionary with the remaining states and their associated capitals in a file called capitals.py capitals_dict = { 'Alabama': 'Montgomery', 'Alaska': 'Juneau', 'Arizona': 'Phoenix', 'Arkansas': 'Little Rock', 'California': 'Sacramento', 'Colorado': 'Denver', 'Connecticut': 'Hartford', 'Delaware': 'Dover', 'Florida': 'Tallahassee', 'Georgia': 'Atlanta', } Next, pick a random state name from the dictionary, and assign both the state and it's capital to two variables. You'll need to import the random module at the top of your program. Then display the name of the state to the user and ask them to enter the capital. If the user answers, incorrectly, repeatedly ask them for the capital name until they either enter the correct answer or type the word 'exit'. If the user answers correctly, display 'Correct' and end the program. However, if the user exits without guessing correctly, display the correct answer and the word "Goodbye". Note: Make sure the user is not punished for case sensivity. In other words, a guess of 'Denver' is the same as 'denver'. Do the same for exiting --- 'EXIT' and 'Exit' should work the same as 'exit'

Solution: https://github.com/realpython/python-basics-exercises/blob/master/ch09-lists-tuples-and-dictionaries/7-challenge-capital-city-loop.py

9.9a- You have 100 cats. One day you decide to arrange all your cats in a giant circle. Initially, none of your cats have any hats on. You walk around the circle 100 times, always starting at the same spot, with the first cat (cat #1). Every time you stop at a cat, you either put a hat on it if it doesn't have one on, or you take its hat off if it has one on. Exercise 1: The first round, you stop at every cat, placing a hat on each one.

Solution: https://github.com/realpython/python-basics-exercises/blob/master/ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats.py

9.9b- You have 100 cats. One day you decide to arrange all your cats in a giant circle. Initially, none of your cats have any hats on. You walk around the circle 100 times, always starting at the same spot, with the first cat (cat #1). Every time you stop at a cat, you either put a hat on it if it doesn't have one on, or you take its hat off if it has one on. Exercise 2: The second round, you only stop at every second cat(#2, #4, #6, #8, etc.).

Solution: https://github.com/realpython/python-basics-exercises/blob/master/ch09-lists-tuples-and-dictionaries/9b-challenge-cats-with-hats.py

9.9c- You have 100 cats. One day you decide to arrange all your cats in a giant circle. Initially, none of your cats have any hats on. You walk around the circle 100 times, always starting at the same spot, with the first cat (cat #1). Every time you stop at a cat, you either put a hat on it if it doesn't have one on, or you take its hat off if it has one on. Exercise 3: The third round, you only stop at every third cat(#3, #6, #9, #12, etc.).

Solution: https://github.com/realpython/python-basics-exercises/blob/master/ch09-lists-tuples-and-dictionaries/9c-challenge-cats-with-hats.py


Kaugnay na mga set ng pag-aaral

Missouri Laws and Rules Pertinent to Insurance

View Set

MCQs in ELECTRICITY AND MAGNETISM

View Set

COM1000 everything everywhere, Public Speaking Chapter 8, COMM chps 1,2,4,6,10, Public Speaking Chapter 10

View Set

Intro to Biology Chapter 3 questions

View Set

Org & Management (Nature and Concept of Management)

View Set

Chapter 3: Anatomy and Physiology of the Reproductive System

View Set

AP Stats- Chapter 9: Testing a Claim

View Set