Skip to main content
 首页 » 编程设计

python之缺少输出,在 Python 中使用 range()

2025年05月04日15itcoder

我没有得到我正在阅读的 Python 书中的章末问题的用户输出。

问题是:

Write a program that counts for the user. Let the user enter the starting number, the ending number, and the amount by which to count.

这是我想出的:

start = int(input("Enter the number to start off with:")) 
end = int(input("Enter the number to end.:")) 
count = int(input("Enter the number to count by:")) 
 
for i in range(start, end, count): 
    print(i) 

在这个输入之后什么都没有发生,除了这个:

Enter the number to start off with:10 
Enter the number to end.:10 
Enter the number to count by:10 

请您参考如下方法:

range(10, 10, 10) 将生成一个空列表,因为 range 从 start 构造了一个 list > 到 stop EXCLUSIVE,所以你要求 Python 构造一个 list 从 10 开始上升到 10 但不包括 10。有10 到 10 之间正好为 0 个整数,因此 Python 返回一个空列表。

In [15]: range(10, 10, 10) 
Out[15]: [] 

没有可迭代的内容,因此循环中不会打印任何内容。