Screw¶
We represent the code of a screw thread as a string that begins with
the character 'M', then follows its diameter (int), a hyphen,
the thread pitch (float), the character ‘x’ and the screw length
(int). All measures are in mm. For example, the thread code
'M3-0.50x10' stands for a thread with a 3mm diameter, a pitch of
0.5mm, and a length of 10 mm. A code is incorrect if it does not begin
with the character 'M' or if it does not contain the characters
'-' or 'x' or they are not in the correct order ('-'
before 'x').
Write a function screw() that takes a string with a thread
code, and returns its measures (diameter, pitch and length). If the
code is not correct, then the function must return the string 'code
error'.
Save this function into the file named screw.py. Examples:
>>> screw('M3-0.50x10') (3, 0.5, 10) >>> screw('M10-0.1x5') (10, 0.1, 5) >>> screw('M14-2.0x125') (14, 2.0, 125) >>> screw('M10-2.0') 'code error' >>> screw('M30.50x10') 'code error' >>> screw('M3x0.50-10') 'code error' >>> screw('A14-2.0x125') 'code error'Note
More tests are provided in the
test-screw.txtfile.
Solution
A solution of these functions is provided in the
screw.py file.