-1

I have just started out learning python and needed some help.

Let's say I have created a simple calculator whereby all it do is just finding the average of the sum.

a = input("First number")
b = input("Second number")
c = input("Third number")

if():
    avg = (a+b+c)/3
print'Average:%.2f' %avg
else:
  ............

How am I supposed to write the if condition to indicate that IF and only if a b and c are int.

I put it as input() because I want to give it an option whereby people who may keyed in something that is not a number such as 'ABC'.

Luke
  • 85
  • 2
  • 2
  • 13
  • 1
    Possible duplicate of [Checking whether a variable is an integer or not](https://stackoverflow.com/questions/3501382/checking-whether-a-variable-is-an-integer-or-not) – VMRuiz Sep 13 '17 at 06:55
  • 1
    Don't use `input` in python2. You should use `a = int(raw_input("First number"))` etc. – John La Rooy Sep 13 '17 at 06:59
  • Well, `input()` returns string, so you can check it using `isdigit()` and if it returns True, then You can convert it to a int – JJAACCEeEKK Sep 13 '17 at 07:00
  • @JohnLaRooy I didn't want it to convert it to INT upon keying in. I am leaving an option whereby people who key in something that is not a number so I will produce an error message out. – Luke Sep 13 '17 at 07:11
  • Possible duplicate of [How to check if string input is a number?](https://stackoverflow.com/questions/5424716/how-to-check-if-string-input-is-a-number) – Gall Sep 13 '17 at 08:03

4 Answers4

0

The if condition to test a, b, c are integers will be:

if (isinstance( a , int ) and isinstance( b , int ) and isinstance( c , int )):

For example , code like this:

a = "asdasd"
b = 2
c = 3
# if(int(a) and int(b) and int(c)):
if (isinstance( a , int ) and isinstance( b , int ) and isinstance( c , int )):
    print("integers")
else:
    print("not integers")

Output for the above example:

not integers
Ahmed Elkoussy
  • 8,162
  • 10
  • 60
  • 85
0
if a.isdigit() and b.isdigit() and c.isdigit():
     avg = (int(a) + int(b) + int(c))/3
JJAACCEeEKK
  • 194
  • 1
  • 11
0

Try this for every variable, first check is it INT , second check is it STR representing INT:

if isinstance(a, int) or (str(a)[0] == '-' and str(a)[1:].isdigit()) or str(a).isdigit():
    print('integer')
else:
    print('not integer')

replace bc if you have integer below zero isdigit() return false. Python 2.7

  • That's an interesting way to check for an integer but the sign can only be the first character and appear once. Right now you will replace multiple characters wherever they are in the string causing false positives. I guess you could go with something like ``(a[0] == '-' and a[1:].isdigit()) or a.isdigit()``. – Gall Sep 13 '17 at 08:15
  • yeah, u right, i missed that there could be multiple '-' at any index, corrected answer – Aleksandr Sokolov Sep 13 '17 at 11:10
-1
import types

if type(a) == types.IntType and type(b) == types.IntType and type(c) == types.IntType:

     ....

     ....
Deepaklal
  • 119
  • 4