Python程序:米和码之间的转换


是国际单位制 (SI) 中的基本长度单位,并在全球范围内使用。它被定义为光在真空中于 1/299,792,458 秒内传播的路径长度。米通常用于科学、工程和日常生活中。

另一方面,是英制和美制常用测量系统中常用的长度单位。它是一个英国单位,定义为 3 英尺或 36 英寸。它被定义为正好等于 0.9144 米。码通常用于日常生活中,例如测量体育、建筑和景观设计中的距离。

米和码都用于测量类似的量,但它们属于不同的测量系统,并且在不同的区域或环境中使用。米和码之间的转换允许将距离从一个单位转换为另一个单位。

在本文中,我们将了解如何使用 Python 编程将米转换为码,以及将码转换为米。

输入输出场景

请查看以下输入输出场景,以了解米到码和码到米的转换 -

场景 1 -

Input meters: 100
Output: 100 meters is equal to 109.36132983377078 yards.
Input yards: 200
Output: 200 yards is equal to 182.88 meters.

场景 2 -

Input meters: 500.5
Output: 500.5 meters is equal to 546.828605 yards.
Input yards: 1000.75
Output: 1000.75 yards is equal to 1094.4335 meters.

数学方法

以下是将米转换为码和将码转换为米的数学公式 -

码到米 -

meters = yards * 0.9144

每码有 0.9144 米。只需将该数字乘以码数即可得到米数。

米到码 -

The formula is yards = meters /0.9144

以码为单位的距离等于以米为单位的距离除以转换因子 0.9144。

示例

这是一个将米转换为码和将码转换为米的示例。

示例

def yards_to_meters(yards): return yards * 0.9144 def meters_to_yards(meters): return meters / 0.9144 meters = 200 print('输入米数:', meters) # 米到码的转换 yards = meters_to_yards(meters) print("输出:{} 米等于 {} 码。".format(meters, yards)) # 码到米的转换 yards = 100 print('输入码数:', yards) meters = yards_to_meters(yards) print("输出:{} 码等于 {} 米。".format(yards, meters))

def yards_to_meters(yards):
    return yards * 0.9144

def meters_to_yards(meters):
    return meters / 0.9144

meters = 200
print('Input meters:', meters)

# Conversion from meters to yards
yards = meters_to_yards(meters)
print("Output: {} meters is equal to {} yards.".format(meters, yards))

# Conversion from yards to meters
yards = 100
print('Input yards:', yards)
meters = yards_to_meters(yards)
print("Output: {} yards is equal to {} meters.".format(yards, meters))

输出

Input meters: 200
Output: 200 meters is equal to 218.72265966754156 yards.
Input yards: 100
Output: 100 yards is equal to 91.44 meters.

示例

这是一个利用字典和字典方法将米转换为码,反之亦然的示例。

在这个示例中,我们将输入距离乘以转换因子 1.09361 以将米转换为码。因为1 米 = 1.09361 码

def convert_distance(distance, conversion_type):
    conversion_factors = {
    'meters_to_yards': 1.09361,
    'yards_to_meters': 0.9144}

    conversion_factor = conversion_factors.get(conversion_type)
    if conversion_factor is None:
        return None
    return distance * conversion_factor

meters = 200
print('Input meters:', meters)
# Conversion from meters to yards
yards = convert_distance(meters, 'meters_to_yards')
print("Output: {} meters is equal to {} yards.".format(meters, yards))

# Conversion from yards to meters
yards = 100
print('Input yards:', yards)
meters = convert_distance(yards, 'yards_to_meters')
print("Output: {} yards is equal to {} meters.".format(yards, meters))

输出

Input meters: 200
Output: 200 meters is equal to 218.72199999999998 yards.
Input yards: 100
Output: 100 yards is equal to 91.44 meters.

更新于: 2023年8月29日

201 次查看

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告