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

Python While循环与枚举问题:代码优化及月份编号实现

问题解答

一、代码优化方案

针对你的代码,我做了以下优化,提升可读性和简洁性:

  • 用f-string替代繁琐的字符串拼接,代码更直观
  • 移除不必要的user_data列表,直接用原始变量生成用户详情
  • 修复拼写错误(如Thanks fot that改为Thanks for that)
  • 统一单位转换逻辑,避免重复计算
  • 避免修改原始weight变量,使用副本进行减重模拟
  • 优化条件判断的表述,减少冗余文本
  • 处理小数显示问题,让输出更友好

优化后的代码:

# 用户信息收集
first_name = input("Hi, what's your name? ")
last_name = input(f'Nice to meet you {first_name}, what\'s your family name? ')
print()
print('Thanks for that, just a few more questions.')

height = int(input('How tall are you (cm)? '))
height_mt = height / 100
age = input('How old are you? ')
weight = int(input("What's your weight in kg? "))

# 展示用户详情
print()
print('Thanks for that. Below your details:')
print()
user_details = (
    f'Name: {first_name}',
    f'Surname: {last_name}',
    f'Height: {height_mt:.2f}mt',
    f'Age: {age}',
    f'Weight: {weight}kg'
)
print('\n'.join(user_details))
print()

# 身高对比逻辑
average_men_height_cm = 174
average_men_height_mt = average_men_height_cm / 100
if height > average_men_height_cm:
    print(f'You are taller than the average man. The average male height worldwide is approximately {average_men_height_mt}mt')
elif height == average_men_height_cm:
    print(f'Your height is in the average range of {average_men_height_mt}mt')
else:
    print(f'You are shorter than the average man. The average male height worldwide is approximately {average_men_height_mt}mt')

# 体重对比逻辑
average_men_weight = 89
if weight > average_men_weight:
    print(f'You weigh more than the average man. The average male weight worldwide is approximately {average_men_weight}kg')
elif weight == average_men_weight:
    print(f'Your weight is in the average range of {average_men_weight}kg.')
else:
    print(f'Your weight is below the average man weight standard. The average male weight worldwide is approximately {average_men_weight}kg.')

print()
# 减重/增重计划计算
weight_difference = weight - average_men_weight
recommended_weight_loss = 2
recommended_weight_gain = 1.5

if weight > average_men_weight:
    weight_loss_timeframe = int(weight_difference / recommended_weight_loss)
    print(f'It seems like you are {weight_difference}kg overweight.')
    print()
    print(f'You need to diet for {weight_loss_timeframe} months.')
    
    # 生成带编号的月份计划表
    print('\n--- 减重计划表 ---')
    current_weight = weight
    for month in range(1, weight_loss_timeframe + 1):
        current_weight -= recommended_weight_loss
        # 确保体重不会低于平均值
        if current_weight < average_men_weight:
            current_weight = average_men_weight
        print(f'Month {month}: Target weight = {current_weight}kg')
else:
    weight_gain_timeframe = int(-weight_difference / recommended_weight_gain)
    print(f'It seems like you are {-weight_difference}kg underweight.')
    print()
    print(f'You need to gain {recommended_weight_gain}kg of muscle a month, for {weight_gain_timeframe} months.')

二、带递增编号的月份计划表实现说明

原来的while循环没有跟踪月份编号,改用for循环更合适:

  1. 先计算需要的总月份数weight_loss_timeframe
  2. 用range(1, weight_loss_timeframe + 1)生成从1开始的月份编号
  3. 每次循环更新当前体重,同时输出带编号的计划条目
  4. 额外处理了体重低于平均值的情况,避免显示不合理的数值

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 00:31:14