0
 user_input = float(input("Please enter a multiplier!")
 if user_input == int:
     print " Please enter a number"
 else:
    for multiplier in range (1,13,1):
        print multiplier, "x", user_input, " = ", multiplier * user_input

The program will run effectively, as for any number entered by the user the result will be effective, yet I wish to know a function that allows the user to ask for a number when they enter a letter.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

3 Answers3

2

Use a try/except inside a while loop:

 while True:
    try:
        user_input = float(raw_input("Please enter a multiplier!"))
    except ValueError:
        print "Invalid input, please enter a number"
        continue # if we get here input is invalid so ask again
    else: # else user entered  correct input
        for multiplier in range (1,13,1):
            print multiplier, "x", user_input, " = ", multiplier * user_input
        break
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
1

Something that's a float is not an int. They're separate types. You can have a float that represents an integral value, like 1.0, but it's still a float.

(Also, user_input == int isn't checking whether user_input is an int, it's checking whether user_input is actually the type int; you wanted isinstance(user_input, int). But since that still won't work, let's skim over this part…)


So, can you check that a float has an integral value? Well, you can do this:

if int(user_input) == user_input

Why? Because 1.0 and 1 are equal, even though they're not the same type, while 1.1 and 1 are not equal. So, truncating a float to an int changes the value into something not-equal if it's not an integral value.

But there's a problem with this. float is inherently a lossy type. For example, 10000000000000000.1 is equal to 10000000000000000 (on any platform with IEEE-854 double as the float type, which is almost all platforms), because a float can't handle enough precision to distinguish between the two. So, assuming you want to disallow the first one, you have to do it before you convert to float.


How can you do that? The easiest way to check whether something is possible in Python is to try it. You can get the user input as a string by calling raw_input instead of input, and you can try to convert it to an int with the int function. so:

user_input = raw_input("Please enter a multiplier!")
try:
    user_input = int(user_input)
except ValueError:
    print " Please enter a number"

If you need to ultimately convert the input to a float, you can always do that after converting it to an int:

user_input = raw_input("Please enter a multiplier!")
try:
    user_input = int(user_input)
except ValueError:
    print " Please enter a number"
else:
    user_input = float(user_input)
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • what if i mean instead an int a str @abarnert because i wish if th e user enters a letter to enter a number instead(wishing a float) and also do you know why in the codeskulptor software says 'Line 1: undefined: [Exception... "Component is not available" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://www.codeskulptor.org/skulpt/skulpt.min.js :: Sk.inputfun :: line 54" data: no]' to the depicted code you created previouusly? – Deborah Israel Sep 28 '14 at 21:55
  • 1
    If you want a `str`, then `raw_input` is all you need; no checking, no converting, you're guaranteed to get a `str`. Meanwhile, I have no idea how to debug problems in a not-quite-Python interpreter on an example I can't see. If you have a new problem, ask a new question, so you can provide complete details. (I think CodeSkulptor has a way to create a "permalink" to an example, doesn't it? Most such sites do… And of course you can put your complete code in the question, too, which is impossible in a comment.) – abarnert Sep 28 '14 at 22:11
-2

You could use the assert-statment in python:

assert type(user_input) == int, 'Not a number'

This should raise an AssertionError if type is not int. The function controlling your interface could than handle the AssertionError e.g. by restarting the dialog-function

MichaelA
  • 1,866
  • 2
  • 23
  • 38
  • This relies on `input()` -- not a good practice in Python 2.x, since it `eval()`s content and so can run arbitrary code. – Charles Duffy Sep 28 '14 at 21:23
  • 2
    Given that `user_input` is guaranteed to be a `float`, this will just always assert, which isn't very useful. Also, `assert` is meant to catch logic errors in the code, like "Shouldn't be able to get here no matter what input we got", not for user errors. – abarnert Sep 28 '14 at 21:27
  • You are right, I missed to part, that she uses float() to directly convert the user input, than my solution would not work. On the usage of assert we seem to disagree a bit. – MichaelA Sep 29 '14 at 07:57