python - 偶数的位数应该是偶数

标签 python

<分区>

我需要打印特定范围内的所有偶数(例如 2000 到 4000)。但是,条件是,“该数字的所有数字都应为偶数”(例如 2222、2468)。在这种情况下,Zero('0') 无关紧要。

我试过这个:

start = int(input("Enter the start of range: ")) 
end = int(input("Enter the end of range: ")) 
for num in range(start, end + 1): 
if num % 2 == 0: 
    print(num, end = " ") 

以上代码只打印所有偶数。

希望大家理解。有什么想法吗...?

最佳答案

如果您对正则表达式解决方案持开放态度,您可以尝试:

for num in range(1, 50): 
    if re.search(r'^[02468]+$', str(num)):
        print(str(num) + " ")

这打印了:

2
4
6
8
20
22
24
26
28
40
42
44
46
48

请注意,我们不必对数字的模二进行额外检查,因为所有偶数位保证数字必须是偶数。

关于python - 偶数的位数应该是偶数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58818603/

相关文章:

python - Matplotlib 中的极地等高线图

python - 如何在 Python 多处理池中运行清理代码?

python - 确定给定的 Python 模块是否是标准库的一部分

python - Python Gekko 中目标函数的约束

python - 如何在python中使用正则表达式?

python - 为什么列表索引必须是整数,而不是元组?

Python 数据框 : how to map a new column from existing column

python - 对时间戳中的泊松过度离散进行建模

python - 如何使用 kazoo 在 Python 中观察后代子节点?

python - Keras:将所有图像保存在一个目录中