Posts: 19
Threads: 7
Joined: Sep 2020
(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()
Posts: 6,823
Threads: 20
Joined: Feb 2020
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?
Posts: 19
Threads: 7
Joined: Sep 2020
Nov-09-2020, 09:09 PM
(This post was last modified: Nov-09-2020, 09:09 PM by KEYS.)
(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.
Posts: 19
Threads: 7
Joined: Sep 2020
(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?
Posts: 8,168
Threads: 160
Joined: Sep 2016
Nov-09-2020, 09:35 PM
(This post was last modified: Nov-09-2020, 09:35 PM by buran.)
(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
Posts: 6,823
Threads: 20
Joined: Feb 2020
Nov-09-2020, 10:22 PM
(This post was last modified: Nov-09-2020, 11:15 PM by deanhystad.)
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?
Posts: 19
Threads: 7
Joined: Sep 2020
(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.
Posts: 6,823
Threads: 20
Joined: Feb 2020
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).
Posts: 8,168
Threads: 160
Joined: Sep 2016
Nov-10-2020, 10:01 AM
(This post was last modified: Nov-10-2020, 10:01 AM by buran.)
(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.
Posts: 19
Threads: 7
Joined: Sep 2020
ok, thank you for the help everyone xd
|