我怎样才能修复这个程序,使它不会返回“未知”?对于负输入(boiling_point)

发布于 2025-01-20 10:24:32 字数 1181 浏览 0 评论 0原文

这是我试图解决的问题的给出的:

*python 编写一个函数,给出从材料到沸点的映射以及沸点温度,将返回:

  • 与沸腾相对应的材料,如果它在 5% 之内差异
  • “未知”,否则

函数签名应如下所示:沸腾_材料(沸腾_映射,沸腾_点)

沸腾_映射示例:给定输入: '丁烷':_0.5, “铜”:1187, “黄金”:2660, “水星”:357, “甲烷”:_161.7, ‘壬烷’:150.8, “银”:2197, 'water': 100}*

使用上述输入运行程序的示例:

输入沸点 >第359章 最接近的物质 : 汞

输入沸点 > 2000年 最接近的材料:未知

我尝试解决它,当输入为正数时它返回正确的输出,但当输入为负数时它只返回“未知”。 示例: 输入沸点> -0.475 预期最接近的材料:丁烷 实际输出:未知

这是我当前的代码。

    boiling_map=dict()
boiling_map['Butane']=-0.5
boiling_map['Copper']=1187
boiling_map['Gold']=2660
boiling_map['Mercury']=357
boiling_map['Methane']=-161.7
boiling_map['Nonane']=150.8
boiling_map['water']=100

def boiling_material(boiling_map,boiling_point):
    closest='unknown'
    for material in boiling_map:
        if abs(boiling_map[material]) >=abs(boiling_point-(boiling_point*5//100)) and abs(boiling_map[material]-boiling_point)<=abs(boiling_point+(boiling_point*5//100)) :
            closest=material
    return closest



print(boiling_material(boiling_map,359))
print(boiling_material(boiling_map,-0.475))
print(boiling_material(boiling_map,0))

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.

    boiling_map=dict()
boiling_map['Butane']=-0.5
boiling_map['Copper']=1187
boiling_map['Gold']=2660
boiling_map['Mercury']=357
boiling_map['Methane']=-161.7
boiling_map['Nonane']=150.8
boiling_map['water']=100

def boiling_material(boiling_map,boiling_point):
    closest='unknown'
    for material in boiling_map:
        if abs(boiling_map[material]) >=abs(boiling_point-(boiling_point*5//100)) and abs(boiling_map[material]-boiling_point)<=abs(boiling_point+(boiling_point*5//100)) :
            closest=material
    return closest



print(boiling_material(boiling_map,359))
print(boiling_material(boiling_map,-0.475))
print(boiling_material(boiling_map,0))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来世叙缘 2025-01-27 10:24:32

您可以做这样的事情。以下是使用Python 3.10.4,3.10以下的任何内容都不支持使用Match关键字。请注意,对于某些测试温度,您会在某些温度尺度上获得已知结果,但在其他温度尺度上没有得到。请注意,这里的结果要么是“未知”或候选人列表。
添加了一种材料和一些测试温度,以使事情变得有趣。

from enum import Enum
import sys;

class TemperatureScale(Enum):
    CELCIUS = 1
    KELVIN = 2
    FAHRENHEIT = 3

# boiling points are in kelvin
materials = { "Butane": 272.65
            , "Copper": 1460.15
            , "Gold": 2933.15
            , "Mercury": 630.15
            , "Methane": 111.45
            , "Nonane": 423.95
            , "Water": 373.15
            , "Propyl Alcohol": 370.65
            }

def kelvin_to_scale(temp, scale):
    match scale:
        case TemperatureScale.KELVIN:
            return temp
        case TemperatureScale.CELCIUS:
            return temp - 273.15
        case TemperatureScale.FAHRENHEIT:
            return 1.8 * (temp - 273.15) + 32
        case _:
            raise Exception('unknown scale')

def scale_to_kelvin(temp, scale):
    match scale:
        case TemperatureScale.KELVIN:
            return temp
        case TemperatureScale.CELCIUS:
            return temp + 273.15
        case TemperatureScale.FAHRENHEIT:
            return (temp*5//9)+459.67
        case _:
            raise Exception('unknown scale')

def test_material(boiling_point, scale = TemperatureScale.KELVIN):
    print(f'Test temperature: {boiling_point} {scale}')
    candidates = []
    for material in materials:
        bp = kelvin_to_scale(materials[material], scale)
        margin = abs(boiling_point)*5//100
        print(f'Testing {material} using {bp} +/- {margin}:\t{bp - margin} <= {boiling_point} < {bp + margin}')
        if (bp - margin <= boiling_point) and (bp + margin > boiling_point):
            candidates += [ material ]
    return candidates

# test values are in celcius
test_values = [ 2801, 359, -0.475, 0, 95 ]

for test_value in test_values:
    kelvin_value = scale_to_kelvin(test_value, TemperatureScale.CELCIUS)
    for scale in [ TemperatureScale.CELCIUS, TemperatureScale.KELVIN, TemperatureScale.FAHRENHEIT ]:
        print(test_material(kelvin_to_scale(kelvin_value, scale), scale) or 'unknown')

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.

from enum import Enum
import sys;

class TemperatureScale(Enum):
    CELCIUS = 1
    KELVIN = 2
    FAHRENHEIT = 3

# boiling points are in kelvin
materials = { "Butane": 272.65
            , "Copper": 1460.15
            , "Gold": 2933.15
            , "Mercury": 630.15
            , "Methane": 111.45
            , "Nonane": 423.95
            , "Water": 373.15
            , "Propyl Alcohol": 370.65
            }

def kelvin_to_scale(temp, scale):
    match scale:
        case TemperatureScale.KELVIN:
            return temp
        case TemperatureScale.CELCIUS:
            return temp - 273.15
        case TemperatureScale.FAHRENHEIT:
            return 1.8 * (temp - 273.15) + 32
        case _:
            raise Exception('unknown scale')

def scale_to_kelvin(temp, scale):
    match scale:
        case TemperatureScale.KELVIN:
            return temp
        case TemperatureScale.CELCIUS:
            return temp + 273.15
        case TemperatureScale.FAHRENHEIT:
            return (temp*5//9)+459.67
        case _:
            raise Exception('unknown scale')

def test_material(boiling_point, scale = TemperatureScale.KELVIN):
    print(f'Test temperature: {boiling_point} {scale}')
    candidates = []
    for material in materials:
        bp = kelvin_to_scale(materials[material], scale)
        margin = abs(boiling_point)*5//100
        print(f'Testing {material} using {bp} +/- {margin}:\t{bp - margin} <= {boiling_point} < {bp + margin}')
        if (bp - margin <= boiling_point) and (bp + margin > boiling_point):
            candidates += [ material ]
    return candidates

# test values are in celcius
test_values = [ 2801, 359, -0.475, 0, 95 ]

for test_value in test_values:
    kelvin_value = scale_to_kelvin(test_value, TemperatureScale.CELCIUS)
    for scale in [ TemperatureScale.CELCIUS, TemperatureScale.KELVIN, TemperatureScale.FAHRENHEIT ]:
        print(test_material(kelvin_to_scale(kelvin_value, scale), scale) or 'unknown')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文