0

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.

asked Sep 21, 2017 at 0:29

3 Answers 3

3

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).

answered Sep 21, 2017 at 0:34
Sign up to request clarification or add additional context in comments.

Comments

1

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.')
answered Sep 21, 2017 at 0:35

1 Comment

I was making it way too complicated for myself. I just removed the line like your example showed and it worked. Thank you!
1

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
answered Sep 21, 2017 at 0:36

1 Comment

Between this and Mike's answer I got it to work. I just ended up removing the line all together and got it working. Thank you!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.