#๐Ÿ”’ How do I get a new output from my function?

51 messages ยท Page 1 of 1 (latest)

lone elbow
#

When set_creator() runs for a second time, it just gives me the same output as the first time it ran. I want it to be a new "instance" with a new output. So every time it runs it asks for the users input again.

https://paste.pythondiscord.com/2VCQ

vital plumeBOT
#

@lone elbow

Python help channel opened

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.

junior dagger
#

Don't global the set list.

#

Return the list you create.

#

Do you know what shadowing is?

lone elbow
junior dagger
#

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.

lone elbow
#

Ah, I see I haven't heard that term but I do know about that. I didn't realize set was a python term.

junior dagger
#

It's a very useful and efficient data structure for the right kinds of tasks.

lone elbow
#

Thanks so much for the help

junior dagger
#

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.

lone elbow
junior dagger
#

You already have a while loop.

#

You may want to give what you feed it on the while line an opportunity to change.

lone elbow
junior dagger
#

Show me the code as it stands.

vital plumeBOT
#

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.
lone elbow
#
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)")

junior dagger
#

I feel as though you may benefit from writing some classes. Have you written your own in Python, yet?

junior dagger
#

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?

lone elbow
junior dagger
#

Do you ever plan on calling armor_select with different arguments?

#
armor_set.append(armor_select(...))```
lone elbow
junior dagger
#

Yes.

lone elbow
# junior dagger 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!

junior dagger
#

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.

lone elbow
#

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!

junior dagger
#

Excelsior.

#

What campaign setting?

lone elbow
# junior dagger What campaign setting?

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.

junior dagger
lone elbow
junior dagger
#

All for now?

#

Because I:ve got nowhere to be if you'd like me to go over classes.

lone elbow
vital plumeBOT
#
Python help channel closed

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.