Your code for calculating the greatest common divisor (GCD) and least common multiple (LCM) of two numbers is mostly correct, but there are a few improvements and corrections that can be made: 1. The `gcd` function should handle cases where either `a` or `b` is zero more gracefully. 2. Using `eval` to get user input is not recommended due to security concerns. Instead, use `int` to convert the input to an integer. 3. Adding some error handling to ensure the inputs are valid positive integers. Here's the improved version of your code: ```python import math def gcd(a, b): while b != 0: a, b = b, a % b return a def lcm(a, b): return abs(a * b) // gcd(a, b) try: num1 = int(input("请输入正整数a:")) num2 = int(input("请输入正整数b:")) if num1 <= 0 or num2 <= 0: raise ValueError("输入必须是正整数。") print("最大公约数:", gcd(num1, num2)) print("最小公倍数:", lcm(num1, num2)) except ValueError as e: print("无效输入:", e) ``` ### Explanation: 1. **GCD Function**: I replaced the `math.gcd` with a custom implementation using the Euclidean algorithm, which is more educational and avoids relying on external libraries. 2. **Input Handling**: Used `int` to convert the input directly to an integer and added error handling to ensure the inputs are positive integers. 3. **Error Handling**: Added a try-except block to catch invalid inputs and provide meaningful error messages. This code will now correctly calculate the GCD and LCM of two positive integers and handle invalid inputs gracefully. [2024-11-25 18:17:48 | AI写代码神器 | 457点数解答]