0

I initially wrote this:

n = input('How many players? ')
while type(n) != int or n <= 2:
    n = input('ERROR! The number of players must be an integer bigger than 2! How many players? ')

and then, after a few lines, this:

V = input("What's the value? ")
while type(V) != int and type(V) != float:
    V = input("ERROR! The value must be expressed in numbers!"+"\n"+"What's the value? ")

And after the first test I realized I need to use raw_input instead of input. But then I need to rewrite the while loops. I don't want the program to break; I want to check the input and send a message error in case the type is not the one asked.

If I use raw_input, how can I check if the input is integer or float, since type(n) and type(V) are both string (using raw_input)?

P.S. For V I want to store the value as an integer if it's an integer and as a float if it's a float

UPDATE: I've solved the problem for the first piece of code like this:

n = None
while n <= 2 :
    try:
        n = int(raw_input('How many players? '))
    except ValueError:
        print 'ERROR! The number of players must be expressed by an integer!'

But I still don't know how to solve the problem for the second piece of code. Unless I trust the user I don't know how to store the value of V so that it will be a float if it's a float or an int if it's an int.

UPDATE #2 - problem solved: For the second piece I came up with these:

while *condition in my program*:
    try:
        V = float(raw_input("what's the value? "))
    except ValueError:
        print "ERROR! The value of the coalition must be expressed in numbers!"
if V - int(V) == 0:
    V = int(V)    

I'm not that happy about the result, but at least it works. Any comments? Suggestions?

Pigna
  • 2,792
  • 5
  • 29
  • 51
  • You get a `str` from `raw_input`. You then need to convert it to whatever type you need, python won't do it on its own. – Paco May 05 '15 at 16:56
  • @Paco: That is not entirely accurate. You are right, if you use python3. However, OP is using python2.7, in which `input` evaluates the user's input before storing in the variable. Python2.7 has `raw_input` as an equivalent of python3's `input` – inspectorG4dget May 05 '15 at 16:59
  • 1
    Not entirely a duplicate because there are things to say about his actual test. Typically you would do the conversion in python and use a try/except construct to see if it succeeds or not. – Sébastien Dawans May 05 '15 at 17:02
  • @inspectorG4dget I realised afterwards that it was a `python-2.7` question, my bad. – Paco May 05 '15 at 18:36

1 Answers1

-2

You can test if a string is a number with:

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

To solve your issue try this

import sys

number = False
while not number:
    value = raw_input("What's the value? ")
    number = is_number(value)
    if not number:
        print >> sys.stderr , 'Error: Admitted only numeric value. Try again' 

After you can test if value is a flaot or a integer and store depending of the result of the built-in function isinstance() in combo with eval() because raw_input returns always a string.

if isinstance(eval(value),float):
    #store the value here as float
    print 'Your input was a float value'
elif isinstance(eval(value),int):
    #store the value here as int
    print 'Your input was a int value'
overcomer
  • 2,244
  • 3
  • 26
  • 39
  • 2
    Oh dear god no. Just stick to specific conversion of int() and float(). – Tymoteusz Paul May 05 '15 at 17:02
  • 2
    For that matter, you could also do this: `type(eval("eval('3')"))`. – Rick May 05 '15 at 17:05
  • 2
    Obligatory suggestion to use [`ast.literal_eval`](https://docs.python.org/2/library/ast.html#ast.literal_eval) instead, which will handle the `int` vs. `float` issue without exposing you to arbitrary input. – jonrsharpe May 05 '15 at 17:07
  • 1
    That's the point, I'm not sure. A reasonable user will obviously input a number. But you never know, and I don't want the program to break, but send an error message. – Pigna May 05 '15 at 17:11