Skip to content
Advertisement

how can I fix this program so it won’t return ‘unknown’ for negative input (boiling_point)

Here’s the given for the problem I’m trying to solve:

*python Write a function that given a mapping from material to boiling points, and a boiling point temperature, will return :

  • the material corresponding to the boiling, if it is within 5% difference
  • ‘UNKNOWN’ otherwise

Function signature should look like : boiling_material(boiling_map, boiling_point)

An example boiling_map : Given input : ‘ Butane’ : _0.5, ‘copper’ : 1187, ‘Gold’ : 2660, ‘Mercury’ : 357, ‘Methane’ : _161.7, ‘Nonane’: 150.8, ‘Silver’ : 2197, ‘water’: 100}*

A sample run of the program with the above input:

Enter boiling point > 359 Closest material : Mercury

Enter boiling point > 2000 Closest material : Unknown

I attempted to solve it and it’s returning the right output when the inputs are positive numbers but it’s only returning ‘unknown’ when the inputs are negative. Example: Enter boiling point > -0.475 Expected Closest material : Butane actual output: Unknown

This is my current code.

JavaScript

Advertisement

Answer

You could do something like this. The below is using Python 3.10.4, anything under 3.10 won’t support using the match keyword. Note that for some test temperatures you get known results on some temperature scales, but not on others. Note that the results here are either “unknown” or a list of candidates. Added a material and some test temperatures to make things interesting.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement