Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Exercise-02 Booleans

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Basic Programming for Non-Informaticians - Practicals

Exercise 2 - Booleans
Hand In
Please, use ILIAS to hand in the exercises or use email if you do not have access to ILIAS yet.
All files should contain your name(s).
Use a separate file for each hand-in task: e.g. exercise01_1_5.py for task 1.5, exercise01_1_6.py for task 1.6, ...
... or hand in this file containing your solutions.

1. Booleans
1. Warm-Up
What is the resulting Boolean value? First discuss with your partner and then check with Python.
True or False

True and False


True and not False

False or (False or True)


(False and True) or (False or True)

2. Hand-In
Write a program that takes any single character as input from the user and checks if it equals "y". Then prints "True" if the
character is not "y".

2. Remainder
1. Warm Up
Write a program that asks the user for a number of seconds and then tells the user how many years, days, hours, minutes, and
seconds that is. For example, if the user enters 365:
That's 0 years, 0 days, 0 hours, 6 minutes and 5 seconds!

Hint
You may assume every year has 365 days

2. Hand In
Write a program that asks the user for an integer and then prints out that integer if it’s odd and prints out 0 if it’s even. Don’t use
booleans and if-else-statements for this task.

3. Warm Up
Also try the opposite from the previous task: Print the integer if the input is even and 0 if it’s odd?

3. if-elif-else
Hand In
Hand in one file for tasks 1-3 and one file for task 4. Or solve each task into a separate jupyter notebook cell.

1.
Write your own desktop calculator in Python. The script should ask for two operands and an operator and the dialog should look
like this:
>>> Operand 1: 5.7
>>> Operator: *
>>> Operand 2: 4.2
5.7 times 4.2 is 23.94

The script should also do something useful if an unknown operator is given. You may assume that the user only inputs numbers
as operands.

2.
Modify your script such that it also works if there are spaces around the operator.

Hint
The string method strip may be of use here.

3.
Modify your script such that it converts the given operands to the correct type (int/float). Comment on why this is desirable.
Is your script also working properly, if the input has spaces in front or after the given operand?

Hint
The string method isdigit may be of use here.

4.
Try to do correct type conversion using only conditional execution and the conversion functions int and `float.
Don't use the string method isdigit!

4. Turtle graphics
Let’s make some drawings. For that we load a module called turtle and then we give this turtle some commands. That could
look like this:
import turtle # This is the "loading" step. Nothing happens on screen!
turtle.forward(100)
turtle.right(90)
turtle.forward(100)

# Keep the window open


turtle.done()

For more commands see https://docs.python.org/3/library/turtle.html

1. Warm Up
Write a program that uses a turtle to draw the figure below.

Hints
Use turtle.begin_fill() and turtle.end_fil()
turtle.circle(radius) accepts negative values for radius
2. Warm Up
Write a program that:
1. asks the user for his favorite color
2. asks the user how many steps the turtle moves to the east,
3. moves the turtle accordingly (let’s say a step is 5 pixels), drawing the line in the users favorite color,
4. asks the user how many times the turtle spins around itself,
5. spins the turtle accordingly,
6. draws a point where the turtle stops and labels that point with its coordinates.

Hints
Use position = turtle.position() to get the x- and the y-coordinates.
Try turtle.write('Hello world!')

5. Lucky numbers
Exam
Your teaching assistant’s lucky numbers are all divisible by 7, but not divisible by 2. Write a program that asks the user for a
number and checks if it is a lucky number. The output must tell the user whether the number is divisible by 2, 7 and whether it is a
lucky number.
>>> Please enter a number: 6
6 is divisible by 2 but not divisible by 7, it is not a lucky number
>>> Please enter a number: 21
21 is not divisible by 2 and divisible by 7, it is a lucky number

Make sure your program checks if the input is an integer and prints an message if not.
>>> Please enter a number: Emil
Emil is not valid decimal

Also make sure your program can handle input with leading and trailing spaces (e.g. ' 12 ')

You might also like