Python Forum
can you call a function from a list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can you call a function from a list?
#11
(Nov-09-2020, 05:32 AM)perfringo Wrote: I have no idea why or what but I can address this 'adding to list' / 'getting entire function output' part:

>>> def my_func():
...     print('Hello world')
...
>>> list_ = []
>>> list_.append(my_func)
>>> list_[0]()
Hello world

This looks like what i may need, but i am getting TypeError: 'builtin_function_or_method' object is not subscriptable

def hall():

    name=''
    inv=[]
    inv.append[trophy(name)]
    name=input('enter name')
    print('you found an item')

    return name,inv

def show(name,inv):
        print(inv[0])
        
def trophy(name):
  sym='.'
  for i in range(1, len(name)+5, 1):
        print (sym, end="")
  print("\n.."+str(name)+"..")
  for i in range(1, len(name)+5, 1):
        print (sym, end="")

def main():
 
        name,inv=hall()
        show(name,inv)
        trophy(name)
main()
Reply
#12
I am leaning towards thinking what you really want are classes. A class is a combination of attributes and behaviors. Your different inventory items probably share similarities, but are also different from each other in some way. Using classes this type of relationship can be represented by having a base class and subclasses.

But before you go off to read all about classes, I think it is worthwhile taking one more shot at arriving at an understanding about the problem you are trying to solve. Could you provide some concrete examples please? What kind of inventory objects are you talking about. What kind of things do you want to do with these inventory objects?
Reply
#13
(Nov-09-2020, 06:28 PM)deanhystad Wrote: I am leaning towards thinking what you really want are classes. A class is a combination of attributes and behaviors. Your different inventory items probably share similarities, but are also different from each other in some way. Using classes this type of relationship can be represented by having a base class and subclasses.

But before you go off to read all about classes, I think it is worthwhile taking one more shot at arriving at an understanding about the problem you are trying to solve. Could you provide some concrete examples please? What kind of inventory objects are you talking about. What kind of things do you want to do with these inventory objects?
Here is part of the assignment.
The key word here for me is that the items and plaque "must be obtainable.

4) Create two loot items/options, they must be obtainable
a) A Gem
i) Use python code to create a for loop to generate a rough picture of a gem. Here is a sample diamond. You can create the gems to look however you like.

b) A plaque with their characters name on it.
i) Code must use loops to encase the character’s name.
ii) The outline should be appropriate in length based on the length of the character’s name

I have created the code for the plaque here

  name='KEYS'
  sym='.'
  for i in range(1, len(name)+5, 1):
        print (sym, end="")
  print("\n.."+str(name)+"..")
  for i in range(1, len(name)+5, 1):
Since this "plaque" must be obtainable i need some way to call on the trophy function and other item functions from a list. So that the user can choose to display the items in the inventory.
Reply
#14
(Nov-09-2020, 05:27 AM)buran Wrote: You can iterate over items in a list and apply a fucntion on each item, no need to have the function in a/the list
e.g.
for item in some_list:
    some_function(item)
or using list comprehension (e.g. if you have a list and want to create new list with elements being the result, i.e. value returned by some function)

This sounds to be what could work. I just do not understand how to implement it. If i am understanding right are you saying i can assign a element name to the function that can be used in the list so that when i call on the list element that corresponds with that function it will in turn call that function?
Reply
#15
(Nov-09-2020, 09:16 PM)KEYS Wrote: are you saying i can assign a element name to the function that can be used in the list so that when i call on the list element that corresponds with that function it will in turn call that function?
I really don't understand what you are asking here

And because this is assignment, I am moving it to Homework
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#16
I think I finally understand what it is you want to do. You want to have a thing that is associated with a function. You would like to be able to say:
ruby = Gem('Ruby')
ruby.draw()
print(ruby.name)
Output:
____ /\/\/\ \ / \ / \/ Ruby
In other words, you want a Class. Have you studied classes in your Python course?
Reply
#17
(Nov-09-2020, 10:22 PM)deanhystad Wrote: I think I finally understand what it is you want to do. You want to have a thing that is associated with a function. You would like to be able to say:
ruby = Gem('Ruby')
ruby.draw()
print(ruby.name)
Output:
____ /\/\/\ \ / \ / \/ Ruby
In other words, you want a Class. Have you studied classes in your Python course?

Yes, and i really like your diamond btw

No, we have not gotten into classes. We are currently learning functions.
Reply
#18
What else do these plaques and gems need to do other than be printed? Maybe a function is enough? As has been demonstrated a variable can reference any Python thing: number, string, class, function. All of these are similar in Python, and you could say that any of them could be "obtainable". You can even make a variable that references a function with arguments (look at functools.partial or lambda functions).
Reply
#19
(Nov-10-2020, 12:27 AM)KEYS Wrote: No, we have not gotten into classes. We are currently learning functions.
If you really not gotten to classes/OOP, then it's not what you need. Just stick to functions. No way they would expect you to create class, when you have not learned about OOP.

(Nov-09-2020, 09:09 PM)KEYS Wrote: Here is part of the assignment.
The key word here for me is that the items and plaque "must be obtainable.

4) Create two loot items/options, they must be obtainable

I think "obtainable" is meant that these are physical objects, i.e. that you can actually draw using ascii art. I think it's meant to prevent that loot item is something like "magic powers" that you really not have a way to "draw".

just my 2 cents. It may be helpful if you post the full text of the assignment, not just excerpts. It may be more clear as to what you are expected to deliver.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#20
ok, thank you for the help everyone xd
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Programming (identifier, literal and function call) ledangereux 5 4,990 May-05-2020, 12:37 PM
Last Post: gumi543
  Calling function-- how to call simply return value, not whole process juliabrushett 2 3,213 Jul-01-2018, 01:17 AM
Last Post: juliabrushett
  Function call Antonio_Gallardo 3 3,202 Dec-29-2017, 11:13 PM
Last Post: Larz60+
  Error is function call Oracle_Rahul 2 3,150 Sep-21-2017, 06:04 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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