You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用循环实现服装尺码计算功能?

如何用循环实现尺码计算逻辑?

我尝试编写下图中的尺码计算逻辑,但仅掌握if else、for和while循环的用法。不清楚如何用循环实现该功能,于是采用if else编写,但仅处理height<155的情况就写了100多行代码。请问能否用循环实现这个功能?

尺码表

以下是我的代码(因篇幅限制仅完成height<155的部分):

height = float(input('Enter height measurement (cm): '))
chest = float(input('Enter Chest Measurement (cm): '))
waist = float(input('Enter waist measurement (cm): '))

if height < 155:
    if chest < 80:
        if waist < 70:
            print('You are size 1 (best fit)')
    elif chest >= 80 and chest < 88:
        if waist <70:
            print('You are size 2 (Regular fit)')
        elif waist >= 76 and waist < 84:
            print('You are size 3 (Relaxed fit)')
        elif waist >= 84 and waist < 92:
            print('You are size 4 (Relaxed fit)')
        elif waist >= 92 and waist < 100:
            print('You are size 5 (Relaxed fit)')
        elif waist >= 100 and waist < 108:
            print('You are size 6 (Relaxed fit)')
        elif waist >= 108 and waist < 116:
            print('You are size 7 (Relaxed fit)')
        elif waist >= 116:
            print('You are size 8 (Relaxed fit)')

    elif chest >= 88 and chest < 96:
        if waist < 70:
            print('You are size 3 (Regular fit)')
        elif waist >= 84 and waist < 92:
            print('You are size 4 (Relaxed fit)')
        elif waist >= 92 and waist < 100:
            print('You are size 5 (Relaxed fit)')
        elif waist >= 100 and waist < 108:
            print('You are size 6 (Relaxed fit)')
        elif waist >= 108 and waist < 116:
            print('You are size 7 (Relaxed fit)')
        elif waist >= 116:
            print('You are size 8 (Relaxed fit)')
    elif chest >= 96 and chest < 104:
        if waist < 70:
            print('You are size 4 (Regular fit)')
        elif waist >= 92 and waist < 100:
            print('You are size 5 (Relaxed fit)')
        elif waist >= 100 and waist < 108:
            print('You are size 6 (Relaxed fit)')
        elif waist >= 108 and waist < 116:
            print('You are size 7 (Relaxed fit)')
        elif waist >= 116:
            print('You are size 8 (Relaxed fit)')
    elif chest >= 104 and chest < 112:
        if waist < 70:
            print('You are size 5 (Regular fit)')
        elif waist >= 100 and waist < 108:
            print('You are size 6 (Relaxed fit)')
        elif waist >= 108 and waist < 116:
            print('You are size 7 (Relaxed fit)')
        elif waist >= 116:
            print('You are size 8 (Relaxed fit)')
    elif chest >= 112 and chest < 120:
        if waist < 70:
            print('You are size 6 (Regular fit)')
        elif waist >= 108 and waist < 116:
            print('You are size 7 (Relaxed fit)')
        elif waist >= 116:
            print('You are size 8 (Relaxed fit)')
    elif chest >= 120 and chest < 128:
        if waist < 70:
            print('You are size 7 (Regular fit)')
        elif waist >= 116:
            print('You are size 8 (Relaxed fit)')
    elif chest >= 128:
        if waist >=116:
            print('You are size 8 (Regular fit)')
        else:
            print('You are size 8 (Relaxed fit)')

解决方案:用循环简化逻辑

当然可以用循环实现,核心思路是把尺码规则整理成结构化的数据列表,然后用循环遍历这些规则,匹配对应的条件即可,不用写大量嵌套的if else。

针对你处理的height < 155的场景,先整理规则:

  1. 胸围按8cm跨度划分区间,每个区间对应基础尺码偏移
  2. 每个胸围区间下,腰围对应不同的尺码和版型

我们可以把规则写成列表,每个元素包含胸围范围、腰围范围、对应的尺码和版型,然后用for循环遍历这些规则,找到匹配项就输出结果。

代码实现

height = float(input('Enter height measurement (cm): '))
chest = float(input('Enter Chest Measurement (cm): '))
waist = float(input('Enter waist measurement (cm): '))

# 整理height <155的尺码规则,每个元素是(胸围下限, 胸围上限, 腰围下限, 腰围上限, 尺码, 版型)
size_rules = [
    # 胸围<80的情况
    (0, 80, 0, 70, 1, 'best fit'),
    # 胸围80-88
    (80, 88, 0, 70, 2, 'Regular fit'),
    (80, 88, 76, 84, 3, 'Relaxed fit'),
    (80, 88, 84, 92, 4, 'Relaxed fit'),
    (80, 88, 92, 100, 5, 'Relaxed fit'),
    (80, 88, 100, 108, 6, 'Relaxed fit'),
    (80, 88, 108, 116, 7, 'Relaxed fit'),
    (80, 88, 116, float('inf'), 8, 'Relaxed fit'),
    # 胸围88-96
    (88, 96, 0, 70, 3, 'Regular fit'),
    (88, 96, 84, 92, 4, 'Relaxed fit'),
    (88, 96, 92, 100, 5, 'Relaxed fit'),
    (88, 96, 100, 108, 6, 'Relaxed fit'),
    (88, 96, 108, 116, 7, 'Relaxed fit'),
    (88, 96, 116, float('inf'), 8, 'Relaxed fit'),
    # 胸围96-104
    (96, 104, 0, 70, 4, 'Regular fit'),
    (96, 104, 92, 100, 5, 'Relaxed fit'),
    (96, 104, 100, 108, 6, 'Relaxed fit'),
    (96, 104, 108, 116, 7, 'Relaxed fit'),
    (96, 104, 116, float('inf'), 8, 'Relaxed fit'),
    # 胸围104-112
    (104, 112, 0, 70, 5, 'Regular fit'),
    (104, 112, 100, 108, 6, 'Relaxed fit'),
    (104, 112, 108, 116, 7, 'Relaxed fit'),
    (104, 112, 116, float('inf'), 8, 'Relaxed fit'),
    # 胸围112-120
    (112, 120, 0, 70, 6, 'Regular fit'),
    (112, 120, 108, 116, 7, 'Relaxed fit'),
    (112, 120, 116, float('inf'), 8, 'Relaxed fit'),
    # 胸围120-128
    (120, 128, 0, 70, 7, 'Regular fit'),
    (120, 128, 116, float('inf'), 8, 'Relaxed fit'),
    # 胸围>=128
    (128, float('inf'), 0, 116, 8, 'Relaxed fit'),
    (128, float('inf'), 116, float('inf'), 8, 'Regular fit'),
]

if height < 155:
    # 遍历所有规则,找到匹配的条件
    for rule in size_rules:
        chest_low, chest_high, waist_low, waist_high, size, fit = rule
        if chest_low <= chest < chest_high and waist_low <= waist < waist_high:
            print(f'You are size {size} ({fit})')
            break

优势说明

  • 代码量大幅减少,逻辑更清晰,后续调整尺码区间只需修改size_rules列表,不用改动大量条件判断
  • 完全基于你掌握的for循环实现,没有引入复杂语法
  • 结构化的规则列表可直接扩展到其他身高区间(如155-170、170以上),只需新增对应规则即可

内容的提问来源于stack exchange,提问作者Wenyong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 17:06:30