#๐ How do I get a new output from my function?
51 messages ยท Page 1 of 1 (latest)
@lone elbow
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Don't global the set list.
Return the list you create.
Do you know what shadowing is?
Thanks for the help! Return the list in the set_creator() function?
So that the call to the function becomes a reference to that list.
var = 123
var = 456```By doing the 456 line, I am shadowing the var name. It exists and I reassign it.
set is a builtin name
It exists as you start up Python.
Avoid shadowing builtins.
Ah, I see I haven't heard that term but I do know about that. I didn't realize set was a python term.
It's a very useful and efficient data structure for the right kinds of tasks.
So when you say to return the list, do you mean in the set_creator() function? Like I should return armor_set (formerly: set)?
Thanks so much for the help
Create the list in the function. Return the list out of the function so the call to the function becomes a reference to that list.```py
def func():
my_list = []
...
return my_list
result = func()```
Also, in the context of your initial code, you weren't reassigning the set name. Thus, you weren't using the functionality of the global keyword.
You can reach outside of scope to look at variables, but as soon as you want to reassign a name outside of scope, you would be using the global or nonlocal keywords.
But you usually shouldn't do that, anyway.
You're recursing.
Best not, but if you do, extend your list with its return or something.
Otherwise you throw away the result.
What should I be doing instead if I want to be able to add multiple results from the same function?
You already have a while loop.
You may want to give what you feed it on the while line an opportunity to change.
Yeah that's what I was hoping to do, but even when I put it outside of the function the return value still seems to become set in stone after the first run. So I must be misunderstanding something I think
Show me the code as it stands.
Hey @lone elbow!
It looks like you're trying to paste code into this channel.
Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
armor_set = []
continue_prompt = "y"
qual_natite = {'material': "Natite", 'strength':14, 'cost': 5}
qual_steel = {'material': "Steel", 'strength':11, 'cost': 3}
all_materials = qual_steel, qual_natite
qual_helmet = {'type':"Helmet",'strength':0.25, 'cost': 2}
qual_chest = {'type':"Chest",'strength':0.35, 'cost': 2}
all_types = qual_helmet, qual_chest
myAC = 0
def type_selector():
type_input = input("What type of armor is it?:")
for types in all_types:
if type_input == types['type']:
return(types)
def material_selector():
material_input = input("What material?:")
for material in all_materials:
if material_input == material['material']:
print ("you have chosen: " + str(material['material']))
print("it has a strength of:" + str(material['strength']))
return material
def armor_select(type_values = type_selector(), material_values = material_selector()):
piece_AC = (material_values['strength'] * type_values['strength'])
piece_price = (material_values['cost'] * type_values['cost'])
full_piece = {'AC_bonus':piece_AC, 'price':piece_price, 'material':material_values['material'], 'type':type_values['type']}
return full_piece
while continue_prompt == 'y':
armor_set.append(armor_select())
print(armor_set)
continue_prompt = input("Would you like to add another piece? (y/n)")
I feel as though you may benefit from writing some classes. Have you written your own in Python, yet?
I haven't, no
Because you look like it you're trying to piece together a type system out of dictionaries.
Do you plan on ever giving arguments to a call of armour_select?
My goal is to have armor select spit out a list of AC_bonus, price, material, and type. Then add that list to a list of all armor pieces that have been created. Then do it again, with new inputs, and add that to the list.
Do you ever plan on calling armor_select with different arguments?
armor_set.append(armor_select(...))```
Oh, I suppose not. I could just put those within the function instead of in the brackets right?
Yes.
ah, that worked. Honestly, I'm not sure why I had it like that in the first place. I think at some point I was thinking you needed them there in order to output them, but that's clearly not the case. Thanks!
Unless you plan for a function to potentially have different information to eat from call to call, a function doesn't need to have parameters.
and for classes, I agree those would be helpful. I'm in a python class and I think we're learning those in a couple weeks. So I'll just stick with this for now, because it's for a DnD session in a few days. Thanks for the help!
My own, called Nalsem. I've been making it for like four years now so it has a lot going on, and I'm always updating the mechanics to be more balanced and fun. It's loosely based on the DnD system but has become very different now.
Classes/types are how Python thinks. The earlier you learn what they are and why we use them, the more Python opens up to you.
I'm excited to learn about them, I'm really enjoying python so far.
Unfortunately I have courses early tomorrow so I was just about to head to sleep now that this is solved. I appreciate the offer though, and if you want to give me a primer here that would be a great help but don't feel obligated. Thanks again!
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.