2017-04-25 97 views
1

這是一個非常愚蠢的問題,但我沒有在一段時間內使用過它,並且不記得如何去做。我正在做一個遞歸問題,並需要顯示數字倒數基本顯示遞歸如何工作。但是,我似乎無法弄清楚。這是我的代碼。Python while loop subtraction

def main(): 

    #have user input first number 
    x = int(input('Enter the first number: ')) 

    #have user input second number 
    y = int(input('Enter the second number: ')) 

    #calculate result by calling recursive function 
    result = mult(x, y) 

    #print the result 
    print(x, 'times', y, 'is', result) 

#define a function that uses recursion 
def mult(x, y): 
    # create a loop to display the numbers 
    count = y 
    while count > 0: 
     print('First number =', x, 'Second number =', count) 
     count -= 1 
    #use recursion to multiply the numbers 
    if x == 0: 
     return 0 
    elif x == 1: 
     return y 
    else: 
     return y + mult(x - 1, y) 

main() 

我需要的輸出這樣說:

Enter the first number: 5 
Enter the second number: 7 
First number = 5 Second Number = 7 
First number = 5 Second Number = 6 
First number = 5 Second Number = 5 
First number = 5 Second Number = 4 
First number = 5 Second Number = 3 
First number = 5 Second Number = 2 
First number = 5 Second Number = 1 
5 times 7 = 35 

所以它的工作大部分,但現在它顯示的是這樣的:

Enter the first number: 5 
Enter the second number: 7 
First number = 5 Second number = 7 
First number = 5 Second number = 6 
First number = 5 Second number = 5 
First number = 5 Second number = 4 
First number = 5 Second number = 3 
First number = 5 Second number = 2 
First number = 5 Second number = 1 
First number = 4 Second number = 7 
First number = 4 Second number = 6 
First number = 4 Second number = 5 
First number = 4 Second number = 4 
First number = 4 Second number = 3 
First number = 4 Second number = 2 
First number = 4 Second number = 1 
First number = 3 Second number = 7 
First number = 3 Second number = 6 
First number = 3 Second number = 5 
First number = 3 Second number = 4 
First number = 3 Second number = 3 
First number = 3 Second number = 2 
First number = 3 Second number = 1 
First number = 2 Second number = 7 
First number = 2 Second number = 6 
First number = 2 Second number = 5 
First number = 2 Second number = 4 
First number = 2 Second number = 3 
First number = 2 Second number = 2 
First number = 2 Second number = 1 
First number = 1 Second number = 7 
First number = 1 Second number = 6 
First number = 1 Second number = 5 
First number = 1 Second number = 4 
First number = 1 Second number = 3 
First number = 1 Second number = 2 
First number = 1 Second number = 1 
5 times 7 is 35 
+0

也請說明什麼輸出是*而不是*。 – mkrieger1

+0

將'print'語句移到if語句上方'mult'函數內部。 – camden

+0

我得到的輸出是這個第一個數字= 5第二個數字= 6的無限循環。它甚至沒有輸出第二個數字作爲第一個數字7並且不低於6 – Classicalclown

回答

0

它看起來像你對我沒有存儲您修改的y值。因此,while循環應該這樣寫:

y_temp = y 
while y_temp > 0: 
    print('First number =', x, 'Second number =', y_temp) 
    y_temp -= 1 
+0

這是工作,但是,現在我的總數是不正確的,因爲它現在認爲y = 0而不是7,所以我的結束輸出是5次0 = 0而不是5次7 = 35 – Classicalclown

+0

將第一個值'y'存儲在另一個變量並在你的最後一次印刷中使用 – Karatawi

+0

@Classicalclown我編輯處理你的評論。 – Vorticity

-1

你忘了更新y,你沒有減少y

0

只需進行一些小的更改即可,您需要更新while loop中的y的值。

#create a loop to display the numbers 
while y > 0: 
    print('First number =', x, 'Second number =', y) 
    y -= 1 
0

while循環是遞歸調用之外,所以y值不會得到更新。將print語句移入mult函數中,並在if語句之上。

def mult(x, y): 
    print('First number =', x, 'Second number =', y) 
    print('First number =', x, 'Second number =', y - 1) 
    if x == 0: 
     return 0 
    elif x == 1: 
     return y 
    else: 
     return y + mult(x - 1, y) 
2

打印這兩個數字的目的是跟蹤遞歸。所以很自然地,這些數字必須從內部的遞歸函數中打印出來。換句話說,你不想創建一個額外的循環(使用while)來顯示數字,但遞歸循環。

你也只想打印一次每個遞歸步驟。

什麼可能另外混淆的是你已交換的「第一」/x和「第二」/y所需的輸出和遞歸的參數順序之間的角色。 (您想在「第二」號在輸出減少,但你減少mult第一參數(x)。)

它應該是這樣的:

def mult(x, y): 
    """Calculate x * y by using recursion.""" 
    print('First number =', x, 'Second number =', y) 
    if y == 0: 
     return 0 
    elif y == 1: 
     return x 
    else: 
     return x + mult(x, y - 1) 

if __name__ == '__main__': 
    x = int(input('Enter the first number: ')) 
    y = int(input('Enter the second number: ')) 

    # calculate result by calling recursive function 
    result = mult(x, y) 

    # print the result 
    print(x, 'times', y, 'is', result) 
+0

哦,你是對的。我記得現在閱讀。你要麼使用循環或遞歸,而不是兩個都對嗎? – Classicalclown

+0

@Classicalclown確切的。 – mkrieger1

+0

好的,謝謝!我忘了那:) – Classicalclown