gpt4 book ai didi

python - 我怎样才能使这个单行程序在 DOS 中工作?

转载 作者:太空狗 更新时间:2023-10-30 00:22:38 24 4
gpt4 key购买 nike

python -c "for x in range(1,10) print x"

我喜欢带有 -c 的 python one liners,但它在需要缩进​​时受到限制。

有什么想法吗?

最佳答案

python -c "for x in range(1,10): print x"

只需添加冒号即可。

解决评论中的问题:

How can I make this work though? python -c "import calendar;print calendar.prcal(2009);for x in range(1,10): print x"

python -c "for x in range(1,10): x==1 and __import__('calendar').prcal(2009); print x;"

如您所见,它非常恶心。我们不能在循环之前导入。为了解决这个问题,我们检查 x 是否在循环的第一次迭代中,如果是,我们进行导入。

更多例子here .

关于python - 我怎样才能使这个单行程序在 DOS 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/566559/

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