Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to make sure that you get a Number Input in Python!!!
dabs364 (277)

Dis is my first tutorial (You have to start somewhere)!!!

I hope you like it and remember to upvote...

It took forever to find out!!!

So lets start!!!

Lets import os and make a clearing function...

import os
def clear():
  os.system("clear")

Then let's start a forever loop to ask for inputs

import os
def clear():
  os.system("clear")
while True:

Now let's use a try and except to ask for inputs that are numbers

import os
def clear():
  os.system("clear")
while True:
  try: 
    clear()
    int(input("Enter a number: "))
  except:
    print("Thats not a number!")

The clear() is the function we made to clear the console

Now let's break out of the forever loop if its a number...

while True:
  try: 
    clear()
    int(input("Enter a number: "))
    break
  except:
    print("Thats not a number!")

Done! Now try your code to see if it works!

Comments
hotnewtop