2013-08-30 117 views
1

使用範圍()我沒有收到在Python的書我讀的結束篇問題的用戶輸出。缺少的輸出,在Python

的問題是:

編寫計算用戶的程序。讓用戶輸入起始數字,結束數字和要計數的數量。

這就是我想出了:

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 

回答

9

range(10, 10, 10)因爲rangestart構建一個list,以將產生一個空列表stopEXCLUSIVE,所以你要求Python構造一個list從10開始到10但不包括10.在10和10之間恰好有0個整數,所以Python返回一個空列表。

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

沒有什麼可以迭代,所以沒有什麼會打印在循環中。

+0

這很有道理。謝謝。 – adam

1

記住range(start, stop, count)始於start,但停止前完成。

所以範圍(10,10,10)將嘗試生產始於10,並停止前10.換句話說沒有什麼不在列表中,並且永遠不會到達打印語句列表。

以其他數字再試一次:開始在5,停止12日之前,通過2計數應該給出更令人滿意的結果。