# **Exercises for Python** --- # Variable ## Input 1. When you distribute information you found from another source, you must always cite your source. The MLA format for a formal citation for a book is as follows: ```python= Last name, First name of author. Title of the Book. Publisher, Year of publication. ``` Write a citation generator. Your program should ask for - The author’s name in the form “Last Name, First Name” - The title of the book - The publisher - The date the book was published Then print out the information in the format shown above. Pay attention to the periods and commas! Here is an example output: ```python= Enter the author's name as 'Last name, First name': Collins, Suzanne Enter the title of the book: The Hunger Games Enter the publisher of the book: Scholastic Enter the year the book was published: 2008 Collins, Suzanne. The Hunger Games. Scholastic, 2008. ``` ## String ## Boolean 1. Ask the user to enter two numbers. Create three boolean variables, as described below. The first should be true if the first number is less than the second number. The second should be true if the first number is equal to the second number. The third should be true if the first number is greater than the second number. Then print the expressions and the result. Here is an example: ``` num1 = 1 num2 = 2 bool1 = num1 < num2 bool2 = num1 == num2 bool3 = num1 > num2 print(bool1, bool2, bool3) ``` 2. **Salt Tax** Ask the user to enter the number of grams of salt they have consumed a day. Using 6 grams as the cut off point, use a boolean statement to state if the user can eat more salt. Sample output: ```python= How many grams of salt have you eaten today? 4 You can eat more sugar: True ``` 3. ## Multiple Variables 1. **Music fan** Create two variables `song` and `artist`. Ask the user what their favorite song is and who is the artist for the song in one line. ## Numerical operator 1. **Internet Cost** A family is paying for an Internet package. The total cost is calculated by the formula `total_cost` = 500 * `hours` + 4500. Calculate the `total_cost` the family has to pay if they use the Internet 50 hours monthly, 62 hours monthly, 96 hours monthly. 2. **Eiffel Tower** Write a program that estimates how many steel bars are needed to construct the Eiffel Tower Assuming that a 10mm steel bar weighs around 7.407kg while the Eiffel Tower comprises of 7,000 metric tons of steel. Calculate the number of steel bars needed. You should use two variables, `eiffel_tower` and `steel_weight`, to calculate the weight. 3. **Sum of the fractions** In this program you will create 4 variables that represent each part of two fractions - The numerator of the first fraction - The denominator of the first fraction - The numerator of the second fraction - The denominator of the second fraction Your program should add the two fractions and print out the result. For example, a sample program run might look like this Note: you do not need to reduce these fractions. Be sure to test your code for positive and negative integers by changing the values of your initialized variables. ```python= The numerator of the first fraction is 1 The denominator of the first fraction is 2 The numerator of the second fraction is 2 The denominator of the second fraction is 5 The sum of 1/2 + 2/5 = 9/10 ``` 4. **Physics: weight = mass * gravity** The weight of an object is defined as the force of gravity on the object and may be calculated as the mass times the acceleration of gravity, **w = mg**. Gravity on Earth (g) = 9,807 m/s². A person weighing 60kg (m) will have a weight of 588N (w) Use `m` as a final variable to solve the following problems. A person weighs 488N on Venus, assuming that the gravity of Venus is 8,87 m/s². What is their mass? What is their weight on Earth? 5. **Supermarket Receipt** (Loop) List objects & units for objects You are going to buy some stuffs in the supermarket. Write a program to help you calculate the total price by asking for the quantity and price of each item. Sample output ```python= How many packs of eggs do you need? 2 How much VND is a pack of egg? 30000 How many cartons of milk do you need? 4 How much VND is a carton of milk? 25000 How many kilogram of tomatoes do you need? 0.8 How much VND is a kilogram of tomato? 15000 Eggs: 60000 VND Milk: 50000 VND Tomato:12000 VND Grand Total: 122000 VND ``` ## Compound Assignment Operator ## Rounding 1. **How many tables?** Three students can share one table. Assuming that there are 37 students in Ms. Xuan's class. At least how many tables would she need so that everyone will have a seat? 2. **Boba tea** A cup of boba tea costs 40000VND. Assume that you have 115000VND. How many cups of boba tea can you buy? ## Temp 1. **ASEAN Football Rankings** The football season is here, and the ranks are out! Unfortunately, the ranks are all shuffled up. If you run the starter code now, you will get the variables in the original order. Your task is to switch the teams around and get them in the new order. You should do this by switching variable values, not just retyping the names. Remember, a variable can only hold one value, so you will need to use a temp variable to help make the switch. For example, if we wanted to switch team1 and team2, we would swap values like this: ``` python= #Swapping values team1 = "A" team2 = "OSU" temp = team2; # team1 = "A", team2 = "OSU", temp = "OSU" team2 = team1; # team1 = "A", team2 = "A", temp = "OSU" team1 = temp; # team1 = "OSU", team2 = "A", temp = "OSU" ``` ``` python= Original order: 1. East Timor U23 2. Myanmar U23 3. Philippines U23 4. Indonesia U23 5. Vietnam U23 New Order/Sample Output: 1. Vietnam U23 2. Indonesia U23 3. Myanmar U23 4. Philippines U23 5. East Timor U23 ``` ```python= #Starter code team1 = "East Timor U23" team2 = "Myanmar U23" team3 = "Philippines U23" team4 = "Indonesia U23" team5 = "Vietnam U23" #Don't edit above this line. #Enter your code below this comment. #Don't edit below this line. #Enter your code above this comment. print("1. " + team1) print("2. " + team2) print("3. " + team3) print("4. " + team4) print("5. " + team5) ``` ## Special character \t, \n # If Statement ## If/Else Statement 1. **Taxi taxi!** ![](https://i.imgur.com/b2GhNEg.png) The Taxi Fare for Mailinh is described below: - For the first 0.3km, charge 5000 VND - From 0.3 to 2km, charge 17400 VND/km - From 3 to 10 km, charge 13100 VND/km - From 11 to 25km, charge 14400 VND/km - From 26km onwards, charge 12000 VND/km Given a certain distance a customer has traveled, using if/elif/else statements, calculate the taxi fare. Sample output ```python= How many kilometers do you travel? 3.7 The taxi fare is 43750 VND ``` 2. **Which bank?** There are 2 saving packages in Nam Á Bank: a 7% annual interest rate, or an immediate payment of 3 million VND with a 6% annual interest rate. Ask the user for their amount of savings and determinethe package that makes the user more well-off 3. Tax Bracket 4. Cooking (learn randint()) Write a program that helps a novice chef decide how long to microwave a refrigerated bread roll. Randomize an integer between 0 and 60 to represent the number of seconds the roll will be microwaved. Print out the number of seconds generated. If the number is less than or equal to 20, print Perfect cooking time! If the number is greater than 20, print Your roll will catch fire! ## Nested If/Else Statement / w Relational Operator 1. Salmon Spawn Every year, salmon return from the salt water they usually live in to a freshwater river to spawn (lay eggs). Some people like to go and watch the salmon swim upstream to their favorite spawning places, especially at fish farms. Your task is to write a program that takes the month of the year as an integer and output if it is “Spring spawning season”, “Fall spawning season”, or “Not spawning season”. The spring spawning season lasts from March to June (month 3 - 6). The fall spawning season lasts from September to November (month 9 - 11). 2. Roller Coaster Write a program that determines if the user can ride a rollercoaster. To ride the rollercoaster, you must be at least 42 inches tall. You must also be at least 9 years old. Ask the user how tall and how old they are. Use a nested if statement to determine if they can ride the rollercoaster. If they can, print “Welcome aboard!” If they cannot, print “Sorry, you are not eligible to ride” ## Short Hand If ... Else # List, Tuple, Set # Loops ## For Loop ## While Loop 1. **The price is right** Let the user guesses the price of an object. If the predicted price is too high, print out "Too high". If the predicted price is too low, print out "Too low" Expected output: ```python= #Assuming that the price is 35000 Guess the correct price of a bowl of Pho. 10000 Too low Guess the correct price of a bowl of Pho. 60000 Too high Guess the correct price of a bowl of Pho. 30000 Too low Guess the correct price of a bowl of Pho. 40000 Too high Guess the correct price of a bowl of Pho. 35000 Correct! ``` ## Break, continue # Functions ## Return # String # Dictionary # Optional: Lambda, Map, etc.