The title is the teacher’s lesson, which is adapted from the python language design basis of Songtian teacher of Beijing Institute of technology, page 4.7, 121
Please modify example 5: body mass index BMI with exception handling, so that it can receive and process any input from the user
while True:
try:
Height, weight = Eval (input (“please input height (m) and weight (kg) [separated by commas]:”)
0 bmi = weight/pow(height,2)
Print (“BMI value is: {. 2F}”. Format (BMI))
0 if height > 3:
if h[:-1].isinstance():
Print (“height value is {. 2F}”. Format (height))
0 break
elif weight > 150:
if w[:-1].isinstance():
Print (“body weight value is {. 2F}”. Format (weight))
0 break
except NameError:
Print (“input error, please input correct information”)
the else:
Print (“no exception occurred”)
finally:
Print (“complete”)
The cause of the error is an operation on an object that cannot be operated on
The original errors were height [: – 1]. Isinstance and weight [: – 1]. Isinstance, which were changed to h and W
Please input height (m) and weight (kg) [comma separated]: 4,85
BMI value: 5.31
input error, please input correct information
complete
please input height (m) and weight (kg) [comma separated]: 1.85,85
BMI value: 24.84
no abnormality
complete
please input height (m) and weight (kg) [comma separated]: 1.85, 200
BMI value: 58.44
input error, please input correct information
complete
please input height (m) and weight (kg) [separated by commas]:
Read More:
- Python PIP TypeError: expected str, bytes or os.PathLike object, not int
- [Solved] TypeError: Object of type ‘bytes’ is not JSON serializable
- Python Openyxl Error: AttributeError: ‘int‘ object has no attribute ‘upper‘ [How to Solve]
- TypeError: Decimal type object is not a JSON serialization solution
- dtype=np.int error: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`.
- Python error: ‘int’ object is not callable
- Typeerror in Python regular expression: expected string or bytes like object
- Python scatter chart error: TypeError: object of type ‘NoneType’ has no len()
- Django Issues: TypeError: “Settings” object is irreversible
- [Solved] TypeError: unsupported operand type(s) for /: ‘str‘ and ‘int‘
- [Solved] error: when using the property decorator in Python, an error occurs: typeerror: descriptor ‘setter’ requires a ‘property’ object but
- Python TypeError: not all arguments converted during string formatting [Solved]
- [Solved] Python Error: TypeError: write() argument must be str, not bytes
- [Solved] python-sutime Error: the JSON object must be str, bytes or bytearray, not ‘java.lang.String‘
- Python Error: ValueError: invalid literal for int() with base 16: ‘ ‘
- ‘coroutine‘ object is not iterable [How to Solve]
- How to Solve Python AttributeError: ‘dict’ object has no attribute ‘item’
- Python error collection: NameError: name ‘numpy’ is not defined
- [Solved] Python3.9 Pycropto RSA Error: TypeError: can’t concat str to bytes
- Python Pandas Typeerror: invalid type comparison