Posts

Showing posts from September, 2021

Python 3.4.1.13 LAB: The basics of lists - the Beatles

Image
 The basics of lists - the Beatles The Basis of Lists in Python If you're taking PCAP - Programming Essentials In Python , you may have encountered this question 3.4.1.13 LAB: The basics of lists - the Beatles. Objectives Familiarize the student with: creating and modifying simple lists; using methods to modify lists. Scenario The Beatles were one of the most popular music group of the 1960s, and the best-selling band in history. Some people consider them to be the most influential act of the rock era. Indeed, they were included in Time magazine's compilation of the 20th Century's 100 most influential people. The band underwent many line-up changes, culminating in 1962 with the line-up of John Lennon, Paul McCartney, George Harrison, and Richard Starkey (better known as Ringo Starr). Write a program that reflects these changes and lets you practice with the concept of lists. Your task is to: step 1 : create an empty list named beatles ; step 2 : use the append() method to...

Python 3.4.1.6 LAB: The basics of lists

Image
 3.4.1.6 LAB: The basics of lists The basics of lists If you're taking PCAP - Programming Essentials In Python , you may have encountered this question 3.4.1.6 LAB: The basics of lists. Objectives Familiarize the student with: using basic instructions related to lists; creating and modifying lists. Scenario There once was a hat. The hat contained no rabbit, but a list of five numbers: 1, 2, 3, 4, and 5. Your task is to: write a line of code that prompts the user to replace the middle number in the list with an integer number entered by the user (Step 1) write a line of code that removes the last element from the list (Step 2) write a line of code that prints the length of the existing list (Step 3) . Ready for this challenge? Solution Code hat_list  = [ 1 ,  2 ,  3 ,  4 ,  5 ]   # This is an existing list of numbers hidden in the hat. # Step 1: write a line...

Python 3.2.1.15 LAB: Collatz's hypothesis

Image
 3.2.1.15 LAB: Collatz's hypothesis  Collatz's hypothesis If you're taking PCAP - Programming Essentials In Python , you may have encountered this question 3.2.1.15 LAB: Collatz's hypothesis. Objectives Familiarize the student with: using the while loop; converting verbally defined loops into actual Python code. Scenario In 1937, a German mathematician named Lothar Collatz formulated an intriguing hypothesis (it still remains unproven) which can be described in the following way: take any non-negative and non-zero integer number and name it c0; if it's even , evaluate a new c0 as c0 ÷ 2 ; otherwise , if it's odd , evaluate a new c0 as 3 × c0 + 1 ; if c0 ≠ 1, skip to point 2 . The hypothesis says that regardless of the initial value of c0, it will always go to 1. Of course, it's an extremely complex task to use a computer in order to prove the hypothesis for any natural number (it may even require artificial intelligence), but you can use Python to check some...

Python 3.2.1.14 LAB: Essentials of the while loop

Image
3.2.1.14 LAB: Essentials of the while loop Essentials of the While Loop If you're taking PCAP - Programming Essentials In Python , you may have encountered this question 3.2.1.14 LAB: Essentials of the while loop. Objectives Familiarize the student with: using the while loop; finding the proper implementation of verbally defined rules; reflecting real-life situations in computer code. Scenario Listen to this story: a boy and his father, a computer programmer, are playing with wooden blocks. They are building a pyramid. Their pyramid is a bit weird, as it is actually a pyramid-shaped wall - it's flat. The pyramid is stacked according to one simple principle:  👉 each lower layer contains one block more than the layer above 👈 . The figure illustrates the rule used by the builders: building blocks Your task is to write a program which reads the number of blocks the builders have, and outputs the height of the pyramid that can be built using these blocks. Note: the height is m...

Python 3.2.1.11 LAB: The continue statement - the Pretty Vowel Eater

Image
 3.2.1.11 LAB: The continue statement - the Pretty Vowel Eater The Pretty Vowel Eater If you're taking PCAP - Programming Essentials In Python , you may have encountered this question in  3.2.1.11 LAB: The continue statement - the Pretty Vowel Eater. Objectives Familiarize the student with: using the continue statement in loops; modifying and upgrading the existing code; reflecting real-life situations in computer code. Scenario Your task here is even more special than before: you must redesign the (ugly) vowel eater from the previous lab (3.1.2.10) and create a better, upgraded (pretty) vowel eater! Write a program that uses: a for loop; the concept of conditional execution (if-elif-else) the continue statement. Your program must: ask the user to enter a word; use user_word = user_word.upper() to convert the word entered by the user to upper case; we'll talk about the so-called string methods and the upper() method very soon - don't worry; use conditional execution and the...

Python 3.2.1.10 LAB: The continue statement - the Ugly Vowel Eater

Image
The continue statement - the Ugly Vowel Eater  the Ugly Vowel Eater If you're taking PCAP - Programming Essentials In Python , you may have encountered this question in 3.2.1.10 LAB: The continue statement - the Ugly Vowel Eater. Objectives Familiarize the student with: using the continue statement in loops; reflecting real-life situations in computer code. Scenario The continue statement is used to skip the current block and move ahead to the next iteration, without executing the statements inside the loop. It can be used with both the while and for loops. Your task here is very special: you must design a vowel eater! Write a program that uses: a for loop; the concept of conditional execution (if-elif-else ) the continue statement. Your program must: ask the user to enter a word; use user_word = user_word.upper() to convert the word entered by the user to upper case; we'll talk about the so-called string methods and the upper() method very soon - don't worry; use conditio...

Python3.2.1.9 LAB: The break statement - Stuck in a loop

Image
 3.2.1.9 LAB: The break statement -  Stuck in a loop The break statement - Stuck in a loop If you're taking PCAP - Programming Essentials In Python , you may have encountered this question in 3.2.1.9 LAB: The break statement - Stuck in a loop. Scenario The break statement is used to exit/terminate a loop. Design a program that uses a while loop and continuously asks the user to enter a word unless the user enters "chupacabra" as the secret exit word, in which case the message "You've successfully left the loop." should be printed to the screen, and the loop should terminate. Don't print any of the words entered by the user. Use the concept of conditional execution and the break statement. 2 Solutions First Solution (with break ) As the exercise asks, you need to include the keyword (break) secret_word  =  "" while   True :      secret_word  =  input ( "You're stuck in an infinite loop! \n Enter a...

Python 3.2.1.6 LAB: Essentials of the for loop - counting mississippily

Image
Essentials of the for loop - counting mississippily If you're taking PCAP - Programming Essentials In Python , you may have encountered this question in 3.2.1.6 LAB: Essentials of the for loop - counting mississippil. Scenario Do you know what Mississippi is? Well, it's the name of one of the states and rivers in the United States. The Mississippi River is about 2,340 miles long, which makes it the second longest river in the United States (the longest being the Missouri River). It's so long that a single drop of water needs 90 days to travel its entire length! The word Mississippi is also used for a slightly different purpose: to count mississippily. If you're not familiar with the phrase, we're here to explain to you what it means: it's used to count seconds. The idea behind it is that adding the word Mississippi to a number when counting seconds aloud makes them sound closer to clock-time, and therefore "one Mississippi, two Mississippi, three Mississip...

Python 3.2.1.3 LAB: Essentials of the While Loop - Guess the Secret Number

Image
Essentials of the While Loop - Guess the Secret Number Guess the secret number If you're taking PCAP - Programming Essentials In Python , you may have encountered this question in 3.2.1.3 LAB: Essentials of the while loop - Guess the secret number Scenario A junior magician has picked a secret number. He has hidden it in a variable named secret_number. He wants everyone who run his program to play the Guess the secret number game, and guess what number he has picked for them. Those who don't guess the number will be stuck in an endless loop forever! Unfortunately, he does not know how to complete the code. Your task is to help the magician complete the code in the editor in such a way so that the code: will ask the user to enter an integer number; will use a while loop; will check whether the number entered by the user is the same as the number picked by the magician . If the number chosen by the user is different than the magician's secret number, the user should see the m...

Python3.1.1.12 LAB: Essentials of the if-elif-else statement

3.1.1.12 LAB: Essentials of the if-elif-else statement If you're taking PCAP - Programming Essentials In Python , you may have encountered this question in 3.1.1.12 LAB: Essentials of the if-elif-else statement: Scenario As you surely know, due to some astronomical reasons, years may be leap or common. The former are 366 days long, while the latter are 365 days long. Since the introduction of the Gregorian calendar (in 1582), the following rule is used to determine the kind of year: if the year number isn't divisible by four, it's a common year; otherwise, if the year number isn't divisible by 100, it's a leap year; otherwise, if the year number isn't divisible by 400, it's a common year; otherwise, it's a leap year. Look at the code in the editor - it only reads a year number, and needs to be completed with the instructions implementing the test we've just described. The code should output one of two possible messages, which are Leap year or Common ...

Python 3.1.1.11 LAB: Essentials of the if-else statement

 PCAP 3.1.1.11 LAB: Essentials of the if-else statement If you're taking PCAP - Programming Essentials In Python , you may have encountered this question in 3.1.1.11 LAB: Essentials of the if-else statement. Scenario Once upon a time there was a land - a land of milk and honey, inhabited by happy and prosperous people. The people paid taxes, of course - their happiness had limits. The most important tax, called the Personal Income Tax (PIT for short) had to be paid once a year, and was evaluated using the following rule: if the citizen's income was not higher than 85,528 thalers, the tax was equal to 18% of the income minus 556 thalers and 2 cents (this was the so-called tax relief) if the income was higher than this amount, the tax was equal to 14,839 thalers and 2 cents, plus 32% of the surplus over 85,528 thalers. Your task is to write a tax calculator. It should accept one floating-point value: the income. Next, it should print the calculated tax, rounded to full ...

Python LAB: Comparison operators and conditional execution

PCAP LAB: Comparison operators and conditional execution If you're taking PCAP - Programming Essentials In Python , you may have encountered this question in LAB: Comparison operators and conditional execution Scenario Spathiphyllum, more commonly known as a peace lily or white sail plant, is one of the most popular indoor houseplants that filters out harmful toxins from the air. Some of the toxins that it neutralizes include benzene, formaldehyde, and ammonia. Imagine that your computer program loves these plants. Whenever it receives an input in the form of the word Spathiphyllum, it involuntarily shouts to the console the following string: "Spathiphyllum is the best plant ever!" Write a program that utilizes the concept of conditional execution, takes a string as input, and: prints the sentence "Yes - Spathiphyllum is the best plant ever!" to the screen if the inputted string is "Spathiphyllum" (upper-case) prints "No, I want a big Spathiphyllu...

Python 2.6.1.11 LAB: Operators and expressions

 A Simple Python Code to Evaluate the End Time If you're taking PCAP - Programming Essentials In Python , you may have encountered this question in 2.6.1.11 LAB: Operators and expressions asking you for a code to evaluate end time. It goes like this: Scenario Your task is to prepare a simple code able to evaluate the end time of a period of time, given as a number of minutes (it could be arbitrarily large). The start time is given as a pair of hours (0..23) and minutes (0..59). The result has to be printed to the console. For example, if an event starts at 12:17 and lasts 59 minutes, it will end at 13:16. Don't worry about any imperfections in your code - it's okay if it accepts an invalid time - the most important thing is that the code produce valid results for valid input data. Test your code carefully. Hint: using the % operator may be the key to success. Solution with Comments: hour = int(input("Starting time (hours): ")) #12 mins = int(input("Starting t...