编写一个程序来求出前n个奇数。
- 定义函数
find_first_n_odds()
,参数为n
。 - 在函数内部,返回前
n
个奇数的列表。 - 解析:
-
def find_first_n_odds(n): list = [] count = 1 for i in range(1,1000): if len(list)<n: list.append(count) count += 2 return list # 获取输入n n = int(input()) # 调用函数 print(find_first_n_odds(n))
记住:len()函数判断长度