Category: Python3

Added: 11th of December 2020

Viewed: 227 times

Learning Python 3, check user input is an integer

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

When the user enters a number or character this is assigned to the variable number.

If the user enters a number, the number is displayed via the print statement. The break statement is used to terminate the current loop

If the user enters a character, a message is then displayed informing the user to an a number. The continue statement is used to return to the beginning of the while loop, and the user has to re-enter a number via the input command.

#!/usr/bin/env python3

while True:

    try:
        number = int(input('Enter a number '))
        print(number)
        break

    except ValueError:
        print("You entered a character. Please only enter numbers")
        continue



Other Tips & Tutorials

Python3

Learning Python 3, check user input is an integer

Posted: 11th of December 2020

Views: 227

Python3

Learning Python3, Generate a random number using Python3

Posted: 8th of December 2020

Views: 210

Python3

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

Posted: 7th of December 2020

Views: 226

Python3

Learning Python3, System calls on Linux

Posted: 1st of December 2020

Views: 221

Python3

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

Posted: 19th of November 2020

Views: 204

Python3

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

Posted: 28th of April 2020

Views: 592