Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a function to create a function in Python
    primarykey
    data
    text
    <p>I'm new to programming and I'm interested in if it's possible to use a function to create another function based in inputted information:</p> <pre><code>def get_new_toy(self): new_toy = gui.multenterbox( msg = 'Enter the data for the new toy:', title = 'New Toy', fields = ('Toy Name', 'Fun for 0 to 5', 'Fun for 5 to 7', 'Fun for 7 to 10', 'Fun for over 10')) method = getattr(PotatoHead, new_toy[0]) def method(self): </code></pre> <p>I don't think that I'm doing this right</p> <p>Thankful for any help</p> <p>---Edit---</p> <p>Sorry for not making it clear:</p> <p>I'm creating a virtual pet sort of game for my younger sister.</p> <p>The 'PotatoHead' can 'play' with certain toys as dictated by functions such as this:</p> <pre><code> def play_Softball(self): self.happiness_num = float(self.happiness) if float(self.age) &lt;= 3.0: self.happiness_num = self.happiness_num + 0.3 elif float(self.age) &gt; 3.0 and float(self.age) &lt; 4.0: self.happiness_num = self.happiness_num + 0.7 elif float(self.age) &gt; 4.0 and float(self.age) &lt; 7.0: self.happiness_num = self.happiness_num + 1.0 elif float(self.age) &gt; 7.0 and float(self.age) &lt; 9.0: self.happiness_num = self.happiness_num + 0.5 elif float(self.age) &gt; 9.0: self.happiness_num = self.happiness_num + 0.02 gui.msgbox( msg = 'This toy is Only providing minimal fun to your potato head. Either get a new one or play with another!', title = 'WARNING!', ok_button = 'Understood') self.happiness = str(self.happiness_num) </code></pre> <p>I want there to be a function that allows the creation of a new 'toy' as a function that is similar to the one above.</p> <p>Again, thanks for any help</p> <p>---UPDATE---</p> <p>Thanks again, but just wondering</p> <p>Is it possible to work something such as this into it:</p> <pre><code>'play_' + new_toy[0] = myPotatoHead.create_toy(new_toy[1], new_toy[2], new_toy[3], new_toy[4], new_toy[5]) </code></pre> <p>in this context:</p> <pre><code> def get_new_toy(self): new_toy = gui.multenterbox( msg = 'Enter the data for the new toy:', title = 'New Toy', fields = ('Toy Name', 'Fun for 0 to 3', 'Fun for 3 to 4', 'Fun for 4 to 7', 'Fun for 7 to 9', 'Fun for over 9')) 'play_' + new_toy[0] = myPotatoHead.create_toy(new_toy[1], new_toy[2], new_toy[3], new_toy[4], new_toy[5]) self.toys.append(new_toy[0]) </code></pre> <p>thanks again for any help</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload