3

I have a very simple problem, when I run the following code:

from random import randint

def create_random():
  x = random.randint(1,4)
  return x 

print(create_random)

The output comes to this:

< function create_random at 0x7f5994bd4f28 >

Note: every character between "0x7f" and "f28" are random each time the code is run.

My goal was to be able to call the function multiple times with x being assigned a new integer value between 1 and 3 with each invocation.

2
  • 5
    You forgot to call the function. Try print(create_random()) instead? Commented Dec 4, 2018 at 7:43
  • FYI: The code as it is won't work. If you do from random import randint you have to use x = randint(1,4) (without the module name). Your call with the module name would run if you did import random. Commented Dec 4, 2018 at 8:33

3 Answers 3

6

You aren't actually calling the function. To do this you need to do:

print(create_random())

Just now you're printing the reference to the function which isn't very helpful for you in this case.

Sign up to request clarification or add additional context in comments.

1 Comment

Yikes. I'm embarrassed to admit I spent over an hour pondering why I had this wrong, I think in my head I assumed I didn't have to call it because there were no parameters. I'm pretty new if you hadn't already guessed that. Thanks though to everyone for their help!
0

You have to call the function, like:

print(create_random())

Also in the function, this line:

x = random.randint(1,4)

Should be just:

x = randint(1,4)

Since you did a from ... import ... statement.

Comments

0

your last line does not do anything, since you want it to print 'create_random' that is not a variable. if you want to call a function, it has to have (). So, you should call it and put it in the print function:

print(create_random())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.