1

I'm writing a program and it asks the user to input a number, I need to make sure that that number is and actual number not a string. That number can be positive or negative. I've tried using .isnumerical() and .isdigit() but they won't except negative numbers.

lowest_num = input("What would you like the lowest possible number to be?") while lowest_num.isdigit() is not True: lowest_num = (input("Please only enter a number : ")).lower() 

Thanks for the help in advance

1

2 Answers 2

0

lowest_num = int(input("What would you like the lowest possible number to be?")) should do it

Alright, try this:

number_not_entered = True num = 0 while number_not_entered: try: num = int(input("enter num")) number_not_entered = False except ValueError: print("please try again") 

Note that catching all exceptions is generally a bad practice.

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

3 Comments

This will not prompt the user for a int until they do, this will just throw up an error and end the program.
it wasn't in your original question, do you think you can update it?
Glad to help :)
0

Use isnan() function from numpy library. First import numpy and then use numpy.isnan(a number)

1 Comment

I tried to import numpy but it told me there wasn't a module named that.FYI I'm using Pycharm 2017.3, don't know if that matters.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.