可以在代码中使用条件语句来判断输入的数字是否是负数,如果是负数则跳过该数字并给出错误提示。以下是Python示例代码:
def add_numbers(numbers): total = 0 for num in numbers: if num < 0: print("Error: Negative numbers not allowed") continue total += num return total
print(add_numbers([5, -3, 8, 2, -9])) # 输出: Error: Negative numbers not allowed 15
上一篇:不发送电子邮件的TFS通知