2014-09-21 123 views
-1

我試圖讓程序向前或向後計數,具體取決於start_number是小於還是大於end_number。它起作用時,start_number較小,但如果它更大,它不會工作。任何想法爲什麼?謝謝!計數遊戲問題

#Challenge 4.1 
#Asks the user for a beginning and ending number, then asks for the amount count by. The  program then prints the numbers 


start_number = int(input("What number would you like me to begin with?")) 
end_number = int(input("What number would you like me to end with?")) 
count_number = int(input("How much would you like me to count by? " + 
        "(Note: for most accurate results have me count by a number " + 
        "that divides evenly into the number you assigned me to end with.)")) 








print("\ncalculating...\n") 

if start_number < end_number: 
    end_number += count_number 
    for number in range (start_number, end_number, count_number): 
     print(number) 

elif end_number < start_number: 
    count_number *= -1 
    for number in range (end_number, start_number, count_number): 
     pyprint(number) 
+0

它只是空白。沒有錯誤信息。 – 2014-09-21 16:44:41

回答

0

您錯誤地顛倒了範圍的參數。修正後,'pyprint'將是一個NameError。你也忽略了調整停止計數。試試這上下。

start = int(input("What number would you like me to begin with?")) 
stop = int(input("What number would you like me to end with?")) 
step = int(input("How much would you like me to count by?")) 

if stop < start: 
    step *= -1 
stop += step 

for number in range (start, stop, step): 
     print(number)