How to write if and else statement if the statement is not a float in python?

77 Views Asked by At

I'm doing a simple calculation again.

import sys

a = float(sys.argv[1])
b = float(sys.argv[2])
c = float(sys.argv[3])
if a == str or  b == str or c == str:
   print 'Your input is invalid'
else:   
    print 'Average: %.2f ' % ((a + b + c) / 3)

or

import sys

a = float(sys.argv[1])
b = float(sys.argv[2])
c = float(sys.argv[3])
if a != float(sys.argv[1]) or  b =! float(sys.argv[2]r or c =! float(sys.argv[3])
   print 'Your input is invalid'
else:   
    print 'Average: %.2f ' % ((a + b + c) / 3)
1

There are 1 best solutions below

1
On
import sys
try:
    a,b,c = nums = [float(a) for a in sys.argv[1:]]
    print 'Average: %.2f' % (sum(nums)/len(nums)))
except:
    print 'Your input is invalid'