Python 3.11.1行星体重转换器:输入特定行星名输出对应结果的问题
行星体重转换器代码问题求助
我是编程新手,正在用Python 3.11.1写一个基于行星重力系数的体重转换器。需求是用户输入体重和行星名后,输出对应行星上的体重,但现有代码有问题:用for循环会输出所有行星的转换结果;换while或if判断又会触发“无效选项”提示,试了各种方法都没解决,求帮忙。
原代码
planets = ['Mercury', 'Venus', 'Earth', 'Moon', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto'] factors = ['0.4155', '0.8975', '1.000', '0.166', '0.3507', '2.5374', '1.0677', '0.8947', '1.1794', '0.0899'] planets_factors = dict(zip(planets, factors)) class WeightConvertor: def __init__(self): self.user_weight = user_weight self.planet = planet def __str__(self): return f"user_weight = {self.user_weight} planet = {self.planet}" def convertor(): print("This choice allows you to see your weight on another planet.") print() print(planets) user_choice = input("Please choose a planet from the list above: ").upper() user_weight = float(input("Please enter your weight here: ")) print() print() planet_choice = [] for user_choice in planets: if user_choice == "Mercury": print("You chose Mercury.") converted_mercury = user_weight * 0.4155 print() print("Your weight on", user_choice, "is", converted_mercury) print() elif user_choice == "Venus": print("You chose Venus.") converted_venus = user_weight * 0.8975 print() print("Your weight on", user_choice, "is", converted_venus) print() elif user_choice == "Earth": print("You chose Earth.") converted_earth = user_weight * 1.000 print() print("Your weight on", user_choice, "is", converted_earth) print() elif user_choice == "Moon": print("You chose Moon.") converted_moon = user_weight * 0.166 print() print("Your weight on", user_choice, "is", converted_moon) print() elif user_choice == "Mars": print("You chose Mars.") converted_mars = user_weight * 0.3507 print() print("Your weight on", user_choice, "is", converted_mars) print() elif user_choice == "Jupiter": print("You chose Jupiter.") converted_jupiter = user_weight * 2.5374 print() print("Your weight on", user_choice, "is", converted_jupiter) print() elif user_choice == "Saturn": print("You chose Saturn.") converted_saturn = user_weight * 1.0677 print() print("Your weight on", user_choice, "is", converted_saturn) print() elif user_choice == "Uranus": print("You chose Uranus.") converted_uranus = user_weight * 0.8947 print() print("Your weight on", user_choice, "is", converted_uranus) print() elif user_choice == "Neptune": print("You chose Neptune.") converted_neptune = user_weight * 1.1794 print() print("Your weight on", user_choice, "is", converted_neptune) print() elif user_choice == "Pluto": print("You chose Pluto.") converted_pluto = user_weight * 0.0899 print() print("Your weight on", user_choice, "is", converted_pluto) print() else: print("Invalid Option") print("Please Try Again") print() user_choice = input("Please choose a planet from the list: ") WeightConvertor.convertor()
问题分析
- 用户输入的
user_choice被转成大写,但后续判断用的是行星原名字符串(如"Mercury"),大小写不匹配导致判断失效 - 没必要遍历所有行星,应该直接用用户输入的选项匹配
- 已经创建的
planets_factors字典未被使用,反而写了大量重复的elif判断,冗余且易出错 convertor方法是类方法但未加@classmethod装饰器,类的__init__方法直接使用未定义的变量,逻辑混乱- for循环的else分支仅在循环正常结束时执行,不是有效的无效输入判断逻辑
修改后的代码
planets = ['Mercury', 'Venus', 'Earth', 'Moon', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto'] # 将系数转为浮点数,方便计算 factors = [0.4155, 0.8975, 1.000, 0.166, 0.3507, 2.5374, 1.0677, 0.8947, 1.1794, 0.0899] planets_factors = dict(zip(planets, factors)) class WeightConverter: @classmethod def convert(cls): print("这个工具可以帮你计算在其他行星上的体重。") print() print("可选行星:", ", ".join(planets)) # 循环获取有效输入,直到用户输入正确的行星 while True: user_choice = input("请选择上述列表中的行星:").strip().capitalize() if user_choice in planets_factors: break print("无效选项,请重新输入!") user_weight = float(input("请输入你的体重:")) print() # 直接从字典获取重力系数 gravity_factor = planets_factors[user_choice] converted_weight = user_weight * gravity_factor print(f"你选择了{user_choice}。") print(f"你在{user_choice}上的体重是{converted_weight:.2f}") WeightConverter.convert()
关键修改点
- 把
factors转为浮点数,避免字符串转浮点的额外操作 - 使用
@classmethod装饰器规范类方法定义 - 用while循环做输入验证,确保用户输入的行星在有效列表内
- 利用
planets_factors字典直接获取系数,删除冗余的elif判断 - 对用户输入做大小写统一处理(
capitalize()),解决大小写不匹配问题 - 输出结果保留两位小数,提升可读性
内容的提问来源于stack exchange,提问作者Cloud
相关产品推荐
相关产品推荐

