0

I was working on a project when I realized that random.randint would not print out.
This is my code:

import random

random.randint(1,25)

In the shell it works, but when I put it into a new file it doesn't work (print/display output).
Does anyone know why? Thanks!

semmyk-research
  • 333
  • 1
  • 9

3 Answers3

1

The line random.randint(1,25) just generates a random number using the function randint() that's inside the random library. This function does not print anything, that's not what it's built for.

If you want to print it, you should use a function like print().

But since this is a basic programming concept, the really useful thing you should do is follow a basic programming course on YouTube or elsewhere.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Thibault Cimic
  • 320
  • 1
  • 9
  • Do you know why this works in IDLE though? – PandaTurtle Jan 28 '23 at 16:24
  • 1
    @PandaTurtle It's not that _it works in IDLE_, but that IDLE has added functionality and it does things without you actually telling it. For example, try this simple two lines program: `2 + 2` ; `print(_)` both as a script and from the console... One of those things is [to output a string representation of non-`None` values](https://docs.python.org/3/library/idle.html#user-output-in-shell). – Ignatius Reilly Jan 28 '23 at 16:49
  • 1
    @PandaTurtle, it's not that it works in IDLE and does not work from the command line in a terminal when executing `python my_code.py`. It's more that the python shell behaviour offered by the IDLE is very learning and teaching oriented, so it has features added to the purpous of better understanding what you just did with this or that line. After all, IDLE stands for "Integrated Development and Learning Environment". But personally I found it quite confusing and I've understood for about coding using a simple text editor and command in the terminal. – Thibault Cimic Jan 29 '23 at 03:49
1

Because you have to print the value because in programming you don't have to print everything so to print you can do the following

print(random.randint(1,25)

or

x=random.randint(1,25)
print(x)
Ali Redha
  • 316
  • 2
  • 14
  • *"because in programming you don't have to print everything"* -- That could be worded clearer, like maybe "Sometimes you want to evaluate an expression but not print what it returns." A good example of that is `file.write(s)`, which writes the string `s` to the file and also returns the number of characters written, which is usually unimportant. – wjandrea Jan 28 '23 at 16:28
  • Yes you are right that will make it clearer but I just wanted somthing simple so that anyone would understand – Ali Redha Jan 29 '23 at 08:19
0

You didn't add "print" method. You have to print your output to see it.

import random
print(random.randint(1,25))

or

import random
random_num = random.randint(1,25)
print(random_num)
  • 1
    Welcome to SO! OP's asking "why", but this doesn't include any explanation. You can [edit] to add one. See [answer]. – wjandrea Jan 28 '23 at 16:23