Practice: functions

Tags
Multiple choice

Finish this.

  • # Function.
  • def lbs_to_kg(lbs):
  •     kg = lbs / 2.2
  •     # What goes here?
  •  
  • # Main program.
  • pounds = float(input('Pounds? '))
  • kilos = lbs_to_kg(pounds)
  • print('Kilos: '+ str(kilos))
Saving
A

return kg

B

return lbs

C

lbs_to_kg = kg

D

return kilos

Not graded. So why do it?

Multiple choice

Finish this.

  • # Function.
  • # What goes here?
  •     if cats > dogs:
  •         message = 'Seriously?'
  •     elif cats == dogs:
  •         message = 'Hmm, could be tricky.'
  •     else:
  •         message = "That's the way!"
  •     return message
  •  
  • # Main program.
  • cattos = float(input('How many cattos?'))
  • doggos = float(input('How many doggos?'))
  • message = compute_happiness_level(cattos, doggos)
  • print(message)
Saving
A

function compute_happiness_level(cats, dogs):

B

def compute_happiness_level(cattos, doggos):

C

def happiness_level(cats, dogs):

D

def compute_happiness_level(cats, dogs):

Not graded. So why do it?

Multiple choice

Complete:

  1. What goes here?:
  2.     state = What goes here?
  3.     if state == 'mi':
  4.         tax_rate = 0.06
  5.     elif state == 'in':
  6.         tax_rate = 0.07
  7.     else:
  8.         tax_rate = 0.0625
  9.     sales_tax = price * tax_rate
  10.     What goes here?
  11.  
  12. def normalize(text):
  13.     text = text.lower().strip()
  14.     return text
  15.  
  16. price = float(input('Price? '))
  17. state_abbrev = input('State (IN, IL, MI)? ')
  18. sales_tax = compute_sales_tax(price, state_abbrev)
  19. total = price + sales_tax
  20. print('Price:'+ str(price))
  21. print('Sales tax (' + state_abbrev + '): '+ str(sales_tax))
  22. print('Total: '+ str(total))
Saving
A
  • def compute_sales_tax(state, price):
  • ...
  • normalize(state)
  • ...
  • return sales_tax
B
  • def compute_sales_tax(price, state):
  • ...
  • normalize(state)
  • ...
  • return price + sales_tax
C
  • def compute_sales_tax(price, state):
  • ...
  • normalize(state_abbrev)
  • ...
  • return sales_tax
D
  • def compute_sales_tax(price, state):
  • ...
  • normalize(state)
  • ...
  • return sales_tax

Not graded. So why do it?

Multiple choice
  • def football_player_total_weight_kg(body_weight, unit):
  •     unit = What goes here?
  •     if unit == 'lb' or unit == 'lbs':
  •         body_weight_kg = What goes here?
  •     else:
  •         body_weight_kg = body_weight
  •     parents_expectations_weight = body_weight_kg * 0.2
  •     total_weight = body_weight_kg + parents_expectations_weight
  •     return total_weight
  •  
  • def lbs_to_kg(lbs):
  •     kg = lbs / 2.2
  •     return kg
  •  
  • def normalize(text):
  •     text = text.lower().strip()
  •     return text
  •  
  • body_weight = float(input('Body weight (lbs or kg)? '))
  • weight_unit = input('Weight unit (lbs or kg)? ')
  • total_weight = What goes here?
  • print('Total weight: '+ str(total_weight))
Saving
A
  • normalize(unit)
  • ...
  • lbs_to_kg(body_weight)
  • ...
  • football_player_total_weight(body_weight_lbs, weight_unit)
B
  • normalize(unit)
  • ...
  • lbs_to_kg(body_weight)
  • ...
  • football_player_total_weight(body_weight_lbs, unit)
C
  • normalize(weight_unit)
  • ...
  • lbs_to_kg(body_weight)
  • ...
  • football_player_total_weight(body_weight_lbs, weight_unit)
D
  • normalize()
  • ...
  • lbs_to_kg(weight)
  • ...
  • football_player_total_weight(body_weight_lbs)

Not graded. So why do it?

Reflect

Write the missing function.

  • import math
  •  
  • ??????
  •  
  • h = length_hypotenuse(3, 4)
  • print(h)

You can use math.sqrt() to find the square root. Feel free to test your code in Spyder before you paste it here.

If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.
Ethan
Ethan

Let's see. I know:

  • The name of the function
  • The number of parameters it takes
  • That there's a return value.

Here's what I have so far.

  • def length_hypotenuse(a, b):
  •     Something
  •     return c

Let me look up the hypotenuse formula, just to check.

Formula

​Hypotenuse formula

OK. I have the sqrt hint. So...

  • c = math.sqrt(something)

Now for the middle bit.

  • c = math.sqrt(a*a + b*b)

Here's the complete thing.

  • import math
  •  
  • def length_hypotenuse(a, b):
  •     c = math.sqrt(a*a + b*b)
  •     return c
  •  
  • h = length_hypotenuse(3, 4)
  • print(h)
Reflect

Write the function evaluate_pokemon for this program:

  1. def normalize(text):
  2.     text = text.lower().strip()
  3.     return text
  4.  
  5. pokemon = input('Pokemon name? ')
  6. evaluation = evaluate_pokemon(pokemon)
  7. print('Pokemon:', pokemon)
  8. print('Evaluation:', evaluation)

Use these evals:

Pokemon Eval
Snorlax That's the best one!
Paras Paras sucks!
Swoobat Yuck!
Anything else OK
If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.
Adela
Adela

Let's see. Line 6...

  • evaluation = evaluate_pokemon(pokemon)

... calls the function. It takes one parameter. To start:

  • def evaluate_pokemon(pokemon):

Line 6 puts the return value into a variable, so:

  • def evaluate_pokemon(pokemon):
  •     Something
  •     return evaluation

OK... Oh! There's normalize, so I'll call that.

  • def evaluate_pokemon(pokemon):
  •     pokemon = normalize(pokemon)
  •     Something
  •     return evaluation

Let's see... there's an eval for Snorlax, so maybe:

  • def evaluate_pokemon(pokemon):
  •     pokemon = normalize(pokemon)
  •     if pokemon == 'Snorlax':
  •         result = "That's the best one!"
  •     return evaluation
Ethan
Ethan

Hey, Adela! normalize makes everything lowercase, so...

Adela
Adela

Oh, right! Test for snorlax, not Snorlax.

  • def evaluate_pokemon(pokemon):
  •     pokemon = normalize(pokemon)
  •     if pokemon == 'snorlax':
  •         result = "That's the best one!"
  •     return evaluation

Thanks!

Now... test for the others...

  • def evaluate_pokemon(pokemon):
  •     pokemon = normalize(pokemon)
  •     if pokemon == 'snorlax':
  •         result = "That's the best one!"
  •     elif pokemon == 'paras':
  •         result = 'Paras sucks!'
  •     elif pokemon == 'swoobat':
  •         result = 'Yuck!'
  •     elseif pokemon == anything else:
  •         result = 'OK'
  •     return result
Adela
Adela

I'm not sure how to test for anything else...

Georgina
Georgina

Maybe use else?

Adela
Adela

Oh! OK.

  • def evaluate_pokemon(pokemon):
  •     pokemon = normalize(pokemon)
  •     if pokemon == 'snorlax':
  •         result = "That's the best one!"
  •     elif pokemon == 'paras':
  •         result = 'Paras sucks!'
  •     elif pokemon == 'swoobat':
  •         result = 'Yuck!'
  •     else:
  •         result = 'OK'
  •     return result

Yay!

Exercise

Exercise

XP

You're a dungeon master. Write a program to compute the XP awarded to players for monsters they've killed. Include this code exactly:

  • kali_score = compute_xp(4, 1, 5)
  • vijay_score = compute_xp(3, 1, 4)
  • mary_beth_score = compute_xp(2, 4, 3)

The first param is the number of orcs killed. They're worth 10 points each.

The second param is the number of goblins killed. They're worth 20 points each.

The third param is the number of giant rats killed. They're worth 7 points each.

If the number of goblins a player kills is at least two more than the number of orcs killed, add 30 XP.

Here's the output your program should produce.

  • Encounter XP
  • ========= ==
  •  
  • Kali: 95
  • Vijay: 78
  • Mary Beth: 151
  •  
  • Party total: 324

Upload a zip of your project folder. The usual coding standards apply.