Lab05 PDF
Lab05 PDF
2022/23 Lab05
Laboratory Exercise 05
Topics
1. Using conditional constructs to make decisions (Lab03 review)
a. Processing and comparisons variables with values of type int, float and str
b. Boolean expressions
c. Logical choices
d. Conditional constructs (if, else, e elif)
e. Blocks and nested instructions
2. Use of loops for repeated execution of instructions (Lab04 review)
a. Loops with while and for statements
b. Complex input processing
c. Simulations
Discussions
A. What is a good strategy for creating a solution to a complex problem?
B. How can I understand if the program implementing the solution is working correctly?
Exercises
Part 1 – Basic loops
Delivery: For each of the following exercises, write a program in Python that responds to the requests
indicated. Complete at least two exercises during the lab session, and the rest at home.
05.1.1 ATM. When you use an automated teller machine (ATM) with your bank card, you need to use
a personal identification number (PIN) to access your account. If a user fails more than three times
when entering the PIN, the machine will block the card. Assume that the user’s PIN is “1234” and write
a program that asks the user for the PIN no more than three times, and does the following:
• If the user enters the right number, print a message saying, “Your PIN is correct”,
and end the program.
• If the user enters a wrong number, print a message saying, “Your PIN is incorrect”
and, if you have asked for the PIN less than three times, ask for it again.
• If the user enters a wrong number three times, print a message saying “Your bank card
is blocked” and end the program.
[P3.39]
05.1.2 Noms des pays. French country names are feminine when they end with the letter e, masculine
otherwise, except for the following which are masculine even though they end with e:
• le Belize
• le Cambodge
• le Mexique
• le Mozambique
• le Zaïre
• le Zimbabwe
1
14BHD Computer science, A.A. 2022/23 Lab05
Write a program that reads the French name of a country and adds the article: “le” for masculine or
“la” for feminine, such as “le Canada” or “la Belgique”.
However, if the country name starts with a vowel, use “l’”; for example, “l’Afghanistan”.
For the following plural country names, use “les”:
• les Etats-Unis
• les Pays-Bas
[P3.30]
05.1.3 Factoring of integers. Write a program that asks the user for an integer and then prints out all
its factors. For example, when the user enters 150, the program should print
2
3
5
5
[P4.16]
05.1.4 At cinema. Write an application to pre-sell a limited number of cinema tickets. Each buyer can
buy as many as 4 tickets. No more than 100 tickets can be sold. Implement a program that prompts
the user for the desired number of tickets and then displays the number of remaining tickets. Repeat
until all tickets have been sold, and then display the total number of buyers.
[P4.33]
where 𝑎 , 𝑏 , 𝑟 and 𝑚 are integers; moreover, the value rnew is equal to rold in the following
computation. Write a program that asks the user to enter an initial value for rold. Then print the first
100 random integers generated by this formula, using a = 32310901, b = 1729, and m = 224.
[P4.27]
05.2.2 The Drunkard’s Walk. A drunkard in a grid of streets randomly picks one of four directions and
stumbles to the next intersection, then again randomly picks one of four directions, and so on. You
might think that on average the drunkard doesn’t move far because the choices cancel each other out,
but that is actually not the case. Represent locations as integer pairs (x, y). Implement the drunkard’s
walk over 100 intersections, starting at (0,0), and print the ending location.
[P4.24]
2
14BHD Computer science, A.A. 2022/23 Lab05
I. a population of prey x increases at a rate dx=Ax dt (proportional to the number of prey) but
is simultaneously destroyed by predators at a rate dx=− B xy dt (proportional to the
product of the numbers of prey and predators);
II. a population of predators y decreases at a rate dy = − C y dt dy = − C y dt (proportional to
the number of predators), but increases at a rate dy = D xy dt (proportional to the product
of the number of prey and predators).
dx/dt =Ax − Bxy
dy/dt =−Cy + Dxy
This means that, considering two time periods n and n + 1, the variation in the number of populations
of prey (x) and predators (y), respectively, from one period to the next is given by:
xn+1 = xn×(1+A−B×yn)
yn+1 = y×(1−C+D×xn)
Write a program that asks the user the input values of the four constants, the initial number of prey
and predators’ populations, and the number of periods to be simulated. After, the program calculates
and display the number of the two populations in each of the periods considered. As test input, use A
= 0.1, B = 0.01, C = 0.01 and D = 0.00002, with initial populations of prey and predators of
1000 and 20, respectively.
[P4.36]
05.2.4 Electrical transformers. Transformers are often built by winding coils of wire around a ferrite
core. The figure illustrates a situation that occurs in various audio devices such as cell phones and
music players and music players. In this circuit, a transformer is used to connect a speaker to the
output of an audio amplifier to the output of an audio amplifier.
The symbol used to represent the transformer is intended to suggest two coils of wire. The parameter
n of the transformer is called the “turns ratio” of the transformer. (The number of times that a wire is
wrapped around the core to form a coil is called the number of turns in the coil. The turns ratio is
literally the ratio of the number of turns in the two coils of wire.)
When designing the circuit, we are concerned primarily with the value of the power delivered to the
speakers—that power causes the speakers to produce the sounds we want to hear. Suppose we were
to connect the speakers directly to the amplifier without using the transformer. Some fraction of the
3
14BHD Computer science, A.A. 2022/23 Lab05
power available from the amplifier would get to the speakers. The rest of the available power would
be lost in the amplifier itself. The transformer is added to the circuit to increase the fraction of the
amplifier power that is delivered to the speakers. The power, Ps, delivered to the speakers is
calculated using the formula
2
𝑛𝑉𝑠
𝑃𝑠 = 𝑅𝑠 ( 2 )
𝑛 𝑅0 + 𝑅𝑠
Write a program that models the circuit shown and varies the turns ratio from 0.01 to 2 in 0.01
increments, then determines the value of the turns ratio that maximizes the power delivered to the
speakers.
[P4.40]