Decimals Stumping Program

Battery_Initial = raw_input("Enter Current Capacity:"))

if Battery_Initial < 0:
    print 'Battery Reading Malfunction'

elif Battery_Initial > 80:
    print 'Battery Reading Malfunction'

This is my program so far. I was wondering if there is a way to only allow inputs such as 0.5, 1.0, 1.5, basically on a 0.5 interval.

Upvotes: 0

Views: 46

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250951

You need to use float() instead of int(), as int() expects decimal input not floats.

Battery_Initial = float(raw_input("Enter Current Capacity:"))

output:

$ python so27.py
Enter Current Capacity:0.5

$ python so27.py
Enter Current Capacity:81.1
Battery Reading Malfunction

$ python so27.py
Enter Current Capacity:-1.2
Battery Reading Malfunction

$ python so27.py
Enter Current Capacity:-1
Battery Reading Malfunction

use something like this:

In [271]: [i/float(2) for i in range(1,10)]
Out[271]: [0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]

usage:

if  Battery_Initial in (i/float(2) for i in range(1,10)):
        #do something here

or as suggested by @Joran Beasley :

if  Battery_Initial % 0.5 ==0:
    #do something here

Upvotes: 2

Related Questions