Python面向过程五十行左右代码
时间: 2024-06-24 13:01:20 浏览: 137
500lines代码,供学习
Python 中的面向过程编程主要关注函数和数据处理,下面是一个简单的示例,通过函数实现一个计算阶乘的过程,大约五十行左右:
```python
def factorial(n):
# 基线条件: 阶乘为1对于0和1
if n == 0 or n == 1:
return 1
# 递归函数: 计算 n 的阶乘
else:
return n * factorial(n - 1)
# 函数应用示例
def process_numbers(numbers):
result = []
for num in numbers:
if isinstance(num, int) and num >= 0:
result.append(factorial(num))
else:
print(f"Invalid input: {num} is not an integer or is negative.")
return result
# 用户输入示例
if __name__ == "__main__":
user_input = [int(i) for i in input("Enter a list of non-negative integers separated by space: ").split()]
print("Factorial of each number:")
print(process_numbers(user_input))
阅读全文