Reputation: 1
I have an exercise for my Python course to right a short program that calculates weight in newtons for the provided mass.
so far I have this
gravity = 9.8
while True:
mass = float(input('Please enter mass of object (in kg): '))
if mass not in range (100, 501):
if mass < 100:
print ('Input too low.')
elif mass > 500:
print ('Input too high.')
else :
break
weight = mass * gravity
print ('the weight of your object is',weight,'newtons.')
It does work as it is, to an extent. Except that it only accepts whole numbers for the input. So inputting 300 works, but inputting 300.x just returns to the prompt for input.
Please enter mass of object (in kg): 317.1231
Please enter mass of object (in kg): 317
the weight of your object is 3106.6000000000004 newtons.
Please enter mass of object (in kg): 317.1
Please enter mass of object (in kg): 317.0
the weight of your object is 3106.6000000000004 newtons.
Where am I going wrong? I specified that the input for mass should be converted to a float, the gravity variable is set to a float.
The only thing I can think of (that just came to me as I wrote this) is that I didn't use floats for the range or < > checks, so it's erroring and returning to the prompt but just doesn't say so because I never set a message for that particular case.
Upvotes: 0
Views: 91
Reputation: 22953
Your problem is that you have a float, but your testing if it's in a range of integers. This will work for cases where the float is an exact value:
Please enter mass of object (in kg): 100.0
('the weight of your object is', 980.0000000000001, 'newtons.')
But fail when it has decimals. You must compare the input directly to the values:
if not 100 <= mass < 501: # range is exclusive.
However, you already check this in your code, so there's no need for the above:
mass = float(input('Please enter mass of object (in kg): '))
if mass < 100:
print ('Input too low.')
elif mass > 500:
print ('Input too high.')
else:
break
Upvotes: 1
Reputation: 4218
Range gives you something similar to a list of numbers, so when you run the line if mass not in range (100, 501):
, you're actually checking if mass
is not in [100, 101, ... , 500]
. This only covers whole numbers, so it won't work.
Instead, just use the comparisons already in your code:
gravity = 9.8
while True:
mass = float(input('Please enter mass of object (in kg): '))
if mass < 100:
print ('Input too low.')
elif mass > 500:
print ('Input too high.')
else :
break
weight = mass * gravity
print ('the weight of your object is',weight,'newtons.')
Upvotes: 1
Reputation: 39838
x not in range(a,b)
doesn't mean the mathematical interval; it checks for being one of the integers in that interval (because that's what range
returns). Using float
arguments to range
won't help; it doesn't allow them, since it's generating discrete results (and there are traps there for the unwary involving floating-point math). Try not a <= x <= b
, which is a nice, standard mathematical syntax supported in Python (but in few other languages).
Upvotes: 3