Calculating BMI
Oppgave
Swipe to start coding
Given dictionary person
with three keys: name
, weight
, and height
. Your tasks are:
- Add new value to
person
dictionary with keybmi
and valueweight/height**2
. - Create pattern named
info
"name weight is weight kg, height is height m. BMI is bmi". Everything there written in italics is the value of aperson
by respective key. - You need to apply
.format()
method toinfo
string passingperson
dictionary as an argument and print the result.
Løsning
99
1
2
3
4
5
6
7
8
9
10
11
# Dictionary
person = {'name': 'John', 'weight': 76, 'height': 1.79}
# Task 1: add bmi to person dictionary
person['bmi'] = person['weight']/person['height']**2
# Task 2: create pattern
info = "{d[name]} weight is {d[weight]} kg, height is {d[height]} m. BMI is {d[bmi]}"
# Task 3: format string info
print(info.format(d = person))
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 3. Kapittel 5
single
99
1
2
3
4
5
6
7
8
9
10
11
# Dictionary
person = {'name': 'John', 'weight': 76, 'height': 1.79}
# Task 1: add bmi to person dictionary
person['___'] = person['weight']/___
# Task 2: create pattern
info = "{d[name]} weight is {___} kg, height is ___ m. BMI is ___"
# Task 3: format string info
print(info.___(_ = ___))
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår