2

How do I create an "if" statement to make sure the input variable is a number and not a letter?

radius = input ('What is the radius of the circle? ') 

#need if statement here following the input above in case user
#presses a wrong key    

Thanks for your help.

Fluxcapacitor
  • 393
  • 2
  • 4
  • 14
  • Related: http://stackoverflow.com/questions/3501382/checking-whether-a-variable-is-an-integer-or-not – Michael Berkowski Jan 04 '13 at 01:15
  • As a side note, `**radius = ...` is no python syntax that I know of. I assume that just put the `**` in there when you posted the question, but for future reference, I'd leave those off and let the StackOverflow markup do it's job :). It's nice if we can copy/paste your code and start to play around without having to guess at what your *actual* code looks like. – mgilson Jan 04 '13 at 02:46
  • the ** before 'radius' happened when i pressed the bold button to highlight the text; that's why you see them again at the end of my post; apologies for the mistake – Fluxcapacitor Jan 04 '13 at 16:47

2 Answers2

6

Assuming you're using python2.x: I think a better way to do this is to get the input as raw_input. Then you know it's a string:

r = raw_input("enter radius:")  #raw_input always returns a string

The python3.x equivalent of the above statement is:

r = input("enter radius:")      #input on python3.x always returns a string

Now, construct a float from that (or try to):

try:
    radius = float(r)
except ValueError:
    print "bad input"

Some further notes on python version compatibility

In python2.x, input(...) is equivalent to eval(raw_input(...)) which means that you never know what you're going to get returned from it -- You could even get a SyntaxError raised inside input!

Warning about using input on python2.x

As a side note, My proposed procedure makes your program safe from all sorts of attacks. Consider how bad a day it would be if a user put in:

__import__('os').remove('some/important/file')

instead of a number when prompted! If you're evaling that previous statement by using input on python2.x, or by using eval explicitly, you've just had some/important/file deleted. Oops.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • that's a good point; anyway, this prog is not being distributed or used by anyone besides me; it's for me to practice learning, but i'll keep your advice for the future – Fluxcapacitor Jan 04 '13 at 16:50
3

Try this:

if isinstance(radius, (int, float)):
    #do stuff
else:
    raise TypeError  #or whatever you wanna do
Volatility
  • 31,232
  • 10
  • 80
  • 89
  • This should be if isinstance(radius, int) or isinstance(radius, float). Alternatively, someone can use if type(radius) in [int, float] which is a bit more elegant. – vkontori Jan 04 '13 at 01:19
  • 1
    @vkontori -- I don't like `type(radius) in (int,float)`, but `isinstance` can check for more than one type. `isinstance(radius,(float,int))` -- you could probably even do a little better with `isinstance(radius,numbers.Number)` – mgilson Jan 04 '13 at 01:21
  • @mgilson -- for the argument type vs isinstance see http://stackoverflow.com/questions/1549801/differences-between-isinstance-and-type-in-python, still the order of the arguments needs to be fixed... – vkontori Jan 04 '13 at 01:26
  • @vkontori -- That post seems to support my comment, but yes, you're definitely correct that the order needed to be fixed. That post is a good find. Posts by Alex are generally well written and informative -- I suppose that's why he's the top python answerer of all time. – mgilson Jan 04 '13 at 01:37