gpt4 book ai didi

Python。更改列表

转载 作者:行者123 更新时间:2023-12-01 02:23:38 24 4
gpt4 key购买 nike

我正在尝试创建一个程序来计算出用户想到的数字。

nums=list(range(1,11))

def remove_odd(l):
return [n for n in l if n % 2 == 0]

y=str(input('Is it an even number?'))
if y=='yes':
(remove_odd(nums))

print(nums)

我得到这个输出:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

但我想得到:

[2, 4, 6, 8, 10]

我做错了什么?

最佳答案

您不会更改列表中的任何内容。将代码更改为:

if y=='yes':
nums = remove_odd(nums)

关于Python。更改列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47673425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com