Category: Python3

Added: 8th of December 2020

Viewed: 343 times

Learning Python3, Generate a random number using Python3

The tutorial presumes you already have Python3 installed on your system. If you don't please visit my other tutorial, How to create a basic Python3 script in Ubuntu and Ubuntu based distributions

The following code below generates a random number from 1 to 10 using Python3.

To generate a random number, we must first import the random module, by entering import random at the top of our script.

We can generate a random number by using the random.randit() function and assign it to the variable random_number

After this we use the print command to display the random number.

Copy and paste the code below

#!/usr/bin/python3
import random

random_number = random.randint(1,10)
print(random_number)


Save the file then enter the following command to make it executable on Linux
chmod +x random_number.py


Then run the file by entering the following in the terminal
./random_number.py

Other Tips & Tutorials

Python3

Learning Python 3, check user input is an integer

Posted: 11th of December 2020

Views: 403

Python3

Learning Python3, Generate a random number using Python3

Posted: 8th of December 2020

Views: 343

Python3

Learning Python 3, getting basic user input and printing the results

Posted: 7th of December 2020

Views: 379

Python3

Learning Python3, System calls on Linux

Posted: 1st of December 2020

Views: 366

Python3

Create a new window using GTK and Python in Ubuntu and Ubuntu based distributions

Posted: 19th of November 2020

Views: 330

Python3

How to create a basic Python3 script in Ubuntu and Ubuntu based distributions

Posted: 28th of April 2020

Views: 769