Reputation: 23
import random
b = random.choice([2309,1897,2307])
def function(user):
user.getApple().eatTheApple(b, 2000, 50, 1, 8663, 4444)
I am fairly new to python but I'm picking it up quickly. B is the variable and it equals a 4 digit number. This 4 digit number will be picked at random.
Then I would like this value to be placed alongside other fixed values such as 2000, 50, 1, 8663 and 4444.
How could I do this? I've been looking around for ages.
Upvotes: 2
Views: 87
Reputation: 5371
Just add b as a parameter of the function, if I understand you correctly.
Consider:
def function(user,b):
user.getApple().eatTheApple(b, 2000, 50, 1, 8663, 4444)
Then we could test with (assuming the class User is defined somewhere else):
import random
myUser = User()
b = random.choice([2309,1897,2307])
function( myUser , b )
We've made a function of two parameters, and passed them both in! An alternative would be:
def function(user):
user.getApple().eatTheApple( random.choice([2309,1897,2307], 2000, 50, 1, 8663, 4444)
I've assumed that User is a class somewhere, so there might be a prettier way to do this yet, if this is appropriate for your circumstances (it might not be). We have our class declaration:
class User:
def __init__( self ):
#this code is executed when the class is created
self.b = random.choice([2309,1897,2307])
def function( self ):
#this code is owned by each User object
user.getApple().eatTheApple(self.b, 2000, 50, 1, 8663, 4444)
That would be executed by:
myUser = User()
myUser.function()
Python likes object-oriented design, so this would be nice! However, it assumes that the "b" value is personal to the user, and doesn't change. I'll give you alternatives, if it does
Upvotes: 2