0% found this document useful (0 votes)
8 views32 pages

Python Programming Assignments Overview

Uploaded by

icke.dsp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views32 pages

Python Programming Assignments Overview

Uploaded by

icke.dsp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd

 declare variables:

- int() to define something as integer (=whole number)


- float() to define something as float (=rational number)
- str() to define something as string (=text)

 ask for input  input()

 ouput  print() output = show a value

literal text always between


(single) quotation marks
‘like this’
Create a repl called Euro-Pound
ASSIGNMENT 1

Convert an amount (entered by the user)


from euro to British pound
€ 1 = £ 0,8834
Create a repl called Fuel consumption
ASSIGNMENT 2

Calculate the cost of a car ride, for which:


- the car uses 6 litres of petrol per 100km
- the price of 1 litre petrol and the distance
driven are entered by the user

TEST VALUES
petrol
price distance

€ 1,785 350
€ 1,612 12
€ 1,987 111
Create a repl called Travel time

The user provides the average speed of a train (in km/h)


ASSIGNMENT 3

and the distance to be travelled (in km).


Calculate the time (in minutes)
to travel the given distance.

TEST VALUES
av.
speed distance

270 100
300 100
180 20
300 350
450 50
Create a repl called Painting

You want to redecorate your bedroom by painting one


ASSIGNMENT 4

wall in a different colour. One litre is enough to paint 7m².


For an ideal coverage you need two layers of paint.
The wall is 2.6m high.
Calculate how much paint (in litres)
you need to paint this wall.
CONDITION
using the IF-function
 a condition is built in the code
 if the condition is met,
the commands in the ‘if block’ are executed
 if the condition is not met,
the commands in the ‘if block’ are skipped (not executed)
 the program continues after the ‘if block’

MIND: sometimes you will have the impression that something


needs to be executed if a certain condition is not met.
The solution here is to reverse the condition.
CONDITION
using the IF-function
don’t forget the colon
if condition:
code to be executed if
the condition is met
rest of the code, not under if!

indentation = very important


LOGICAL OPERATORS
that can be used to build a condition

< smaller than


15<10  FALSE

> bigger than


15>10  TRUE

<= smaller than or equal to 2<=4


 TRUE

>= bigger than or equal to 80>=80


 TRUE
ARITHMETIC OPERATORS
that can be used for mathematical operations

+ addition
8 + 3  11

- substraction
8–35

* multiplication
8 * 3  24

/ division
8 / 3  2,66666666
Create a repl called CinemaTicket
The user enters the result they scored on a test and the
ASSIGNMENT 5

maximum score they could achieve. The percentage is


calculated and shown to the user. Only if the result is 80%
or more, a message is shown that he/she wins a cinema
ticket.
TEST VALUES
score maximum
16 20
23 40
10 15
75 90
Create a repl called DoubleGlazing

The government implemented a measure to save energy:


ASSIGNMENT 6

those who install double glazing will receive


40% of the cost back, with a maximum of €600.

The user enters the total cost and you calculate the refund.
Of course, whe the refund exceeds €600, you limit it to €600.

TEST VALUES
cost
€ 1000
€ 2500
€ 750
€ 2000
Create a repl called Fine

You design a program that calculates the fine.


ASSIGNMENT 7

The user enters his/her speed and the maximum


allowed speed. If the user was not driving too fast,
there is no problem and the prgram should do nothing.
If the user drove too fast, you calculate the fine as
follows: a fixed amount of €100 + €10 per km that the
user drove above the maximum speed.

Don’
t forg
to te et
st!
RECAP
 always put literal tekst between quotation marks!
 declaring variables: name=value
 variable types:
- int() to define something as integer (= whole number)
- float() to define something as float (= rational number)
- str() to define something as string (= text)
 ask for input  input()
 output  print() join elements by using , (comma)
 use if to build a condition, don’t forget indentation!
score=int(input("What's your score?"))
max=int(input("What's the maximum score?"))
EXAMPLE percent=score/max*100
print("That is",percent,"%")
if percent<50:
print("This means you didn't pass the
test")
CONDITION
boolean
if condition:
code to be executed if the condition is
the condition is met TRUE

else:
everything that needs to be the condition is
exe- FALSE
cuted if the condition is not
met
don’t forget
indentation
Create a repl called OddOrEven

Determine whether a number (input by the user)


ASSIGNMENT 8

is odd or even, and show this to the user.


 Don’t forget to convert the input
from text to a number by using int()
 To calculate the remainder of a division, you
can use the modulus function, for example:
Remainder=Number%2
Create a repl called Thunderstorm
Determine how far a thunderstorm is and how the user
reacts best. The user enters the number of seconds between
ASSIGNMENT 9

lightning and thunder. The distance of the thunderstorm =


speed of sound (343m/s) x the counted time (in seconds)
If the thunderstorm is more than 5 kilometres away,
the user gets the message ‘Walk to a safe place quickly’.
If it’s less than 5km, the message is ‘Lie flat on the ground’.

TEST VALUES
number of seconds
12
5
26
18
Create a repl called Operations

Have the user enter two numbers. After that, you


show all the operations (addition/substraction/
ASSIGNMENT 10

multiplication/division) with these two numbers.


For the substraction you deduct the smallest number
from the largest number. For the division you divide
the largest number by the smallest number.
Tip: you can use \n in Python to create a new line in
the output. Mind that you need to put this between
quotation marks as (part of) literal text!
TEST VALUES
number 1 number 2
20 5
25 11
3 19
1 1
CONDITION
using multiple conditions
if condition: don’t forget
code to be executed if indentation
the condition is met
elif second condition:
code to be executed if the
second condition is met
else:
everything that needs to be
executed
Create a repl called GuessTheNumber
The computer has a number between 0 and 10
‘in mind’. The user tries to guess this number.
ASSIGNMENT 11

There are three possible responses from the computer:


• Correct, you win!
Look at the ne
• Higher! xt slide first
for an import
ant note!
• Lower!

In any case the computer shows the number


that had to be guessed after this message.
the function randint()
You can get a random number from the computer using the
function randint(). The ‘problem’ is that this function is not
present by default in Python. Therefore, we will ‘load’ it from
the random library, using this code:
from random import randint
In the next line, you use the function to create a variable. In
between brackets you set the minimum and maximum for
the random number.

number=randint(50,100)
for example will create the variable number and
assign a random number to it between 50 and 100.
Create a repl called BMI

The user enters his/her weight (in kg) and length (in m).
ASSIGNMENT 12

The computer then calculates the Body Mass Index and


shows a message:
BMI below 20  underweight
BMI above 25  overweight
BMI between 20 and 25  ideal BMI

BMI = weight (in kg) / length (in m) x length (in m)


DEFINITE ITERATION (LOOP)
With for you can repeat a piece of code a predefined
number of times in Python, so that you only have to
type this command once in your code.

First you create a variable (for example: i)


and then you use the following syntax:

for <variable> in <collection>:

The collection is how many times you want the loop to repeat.
You could use range(10) for example if you want something to
repeat ten times. The final code would then look like this:

for i in range(10):
Create a repl called Multiplication
ASSIGNMENT 13

Set up the multiplication table for any number entered


by the user, in which you multiply this number by 1-10.
Create a repl called Dice
ASSIGNMENT 14

The user (virtually) rolls two dice, 10 times. What the


user has thrown is shown each of those ten times.
Think carefully about how you translate rolling two
dice into code. The result can vary from 2 to 12.

In the end, the total number of eyes thrown with the


dice is shown. Also the average number of eyes of
those ten throws is shown.
Create a repl called Fibonacci

You let the computer display the first twenty numbers


ASSIGNMENT 15

of the Fibonacci sequence by using the for-function.


The result is shown as follows:
'Number 1 = ...', 'Number 2 = ...', etc.
! This seems like a very easy exercise, but it is
absolutely not. So first think very carefully (= analysis!)
before you start working on your code !
CONDITIONAL ITERATION (LOOP)
With WHILE you can repeat a piece of code time
in Python for as long as a certain condition is met.
(contrary to for = loop a fixed number of times)
First you create a variable (for example: i)
and then you use the following syntax:

while <condition with the variable>:

MIND: in a conditional loop, you have to make sure that the


condition is changed by the code executed in the loop.
Otherwise you create an ifinite loop, which is not what you want.
If you want to repeat something 10 times for example,
the code would look like this:
while i < 10:
Create a repl called Elephants

There are about 104 000 African forest elephants left.


ASSIGNMENT 16

The population will grow at 7% per year if people can


protect the elephants from poachers and the destruction
of their habitat.
Create a program that calculates how many years it takes for
the population to double in size, using a conditional loop.
Let’s change that print!
SEP END
With the function sep you can With the function end you can
instruct Python which separator to change how a print ends. Standard,
use between multiple prints. To use this is a new line. To use end, you
sep, you need to put it in the print need to put it in the print function
function and have it followed by = and have it followed by = and the
and the separator you wish to use ending you wish to use between
between quotation marks. quotation marks.
example: example:
print(“first”,“second”,sep=“ > ”) print(“this is”,end=“ ”)
result: print(“some text”)
first > second result:
this is some text
Create a repl called Numbers

Create a program that lets the user input two numbers


ASSIGNMENT 17

(x and y). The program then shows x and y and all


numbers in between, either counting up or down,
depending on whether the first number that was
entered (x) is the biggest of the two or not.
All numbers should appear on one line,
separated by spaces.
ex.: input = 5 & 11 result = 5 6 7 8 9
10 11
input = 11 & 5 result = 11 10 9 8 7 6 5
Create a repl called StartToRun
Create a program that calculates how many days
ASSIGNMENT 18

you have to train to run a certain distance.


The user enters the distance he/she runs on the first
training day. Each following day, you can run 10% further
than the day before. The user also enters the target
distance (in km).
The program shows how many days you have to
train before you can run the target distance.
Create a repl called Apples
Create a program that calculates how many apples
ASSIGNMENT 19

each pupil gets. Therefore, the user enters the number


of apples and the number of pupils.
Each pupil gets the same number of apples.
Let the program show the number of apples that each pupil
gets, and the number of apples that is not used.
Create a repl called ReturnMoney
ASSIGNMENT 20

Create a program asks the user the price that


needs to be paid and the money that was given.
The program then returns how much money needs to
be returned, specified in the different notes and coins.

You might also like