Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Calculating BMI | Strings Formatting
String Manipulation in Python

book
Calculating BMI

Oppgave

Swipe to start coding

Given dictionary person with three keys: name, weight, and height. Your tasks are:

  1. Add new value to person dictionary with key bmi and value weight/height**2.
  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 a person by respective key.
  3. You need to apply .format() method to info string passing person dictionary as an argument and print the result.

Løsning

# 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?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 5
single

single

# 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

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

some-alt