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?