Python Forum
Getting error trying to use functions to multiply numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting error trying to use functions to multiply numbers
#5
1. Well, yes, of course, just call the functions on line 21 so that their return values are passed to calculate_result

2. When you use the function name without parentheses, that refers to the function itself. It does not call the function. Being able to pass functions around to other functions is very useful. For example, when you want to do something when a button is pressed on a UI, you express the "something" as a function, but you don't call it - you give that function to the library code that handles the button press. It's again, all about parameterising the thing to be done. Other examples are when you want to operate on collections, e.g.

- doing the same thing to every item in a collection:

def square(x):
    return x ** 2

values = [1, 2, 3]
squared_values = map(square, values)
names = ["alice", "bob", "mary"]
capitalised_names = map(lambda name: name.title(), names)
- keeping items in a collection that satisfy a condition

def even(x):
    return x % 2 == 0

values = [3, 2, 4, 7, 20, 16]
evens_only = filter(even, values)
names = ["Alice", "Mary", "Bob", "Andrew"]
names_starting_with_a_only = filter(lambda name: name.startswith("A"), names)
Reply


Messages In This Thread
RE: Getting error trying to use functions to multiply numbers - by ndc85430 - Aug-30-2021, 07:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Error : "can't multiply sequence by non-int of type 'float' " Ala 3 3,167 Apr-13-2021, 10:33 AM
Last Post: deanhystad
  I want to multiply two variables, but I get an error LeqendFire 3 2,250 Aug-09-2020, 03:52 PM
Last Post: LeqendFire
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,852 May-09-2019, 12:19 PM
Last Post: Pleiades

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020