# Python程序将米转换为码，反之亦然

## 输入输出场景

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


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


The formula is yards = meters /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.


### 示例

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.


• 回顶