2015-09-20 28 views
-1

我正在編寫一個程序,允許用戶輸入一個數字範圍,然後該程序將執行該範圍內每個數字的冰雹序列,然後將打印出數量最大的數字週期長度。我不明白爲什麼我的代碼不工作。我們需要使用while循環使用嵌套while循環的冰雹序列

def main(): 
    #set starting variables 
    start_num = int(input('Enter starting number of the range: ')) 
    #check if the numbers entered are positive and that the start is less than the end 
    while (start_num < 1): 
      start_num = int(input('Enter a positive starting number of the range: ')) 
    end_num = int(input('Enter ending number of the range: ')) 
    while (end_num < 1): 
      end_num = int(input('Enter a positive ending number of the range: ')) 

    while (start_num > end_num): 
      start_num = int(input('Enter starting number of the range: ')) 
      end_num = int(input('Enter ending number of the range: ')) 

    cycle_length = 0 
    max_length = 0 
    num_max = 0 
    num = 0 

    while (start_num < end_num): 

      while (num != 1): 

        if (start_num % 2 == 0): 
          num = start_num/2 
          cycle_length = cycle_length +1 
        else: 
          num = (start_num * 3) + 1 
          cycle_length = cycle_length +1  

        if (cycle_length >= max_length): 
          max_length = cycle_length 
          num_max = start_num 

        cycle_length = 0 
      start_num = start_num + 1 

    print(num_max) 
    print(max_length) 

main() 

回答

2

在你while循環,你總是檢查start_num,這永遠不會改變。在循環開始時,您需要將num設置爲start_num。然後在整個循環體中使用num

+0

即使這樣做,程序不打印過什麼。它只是無限期地運行。 – Rsherrill

+0

請發佈您的修改代碼。 – saulspatz

0

我只是要通過每一行,並告訴你什麼是錯的。你應該確保你知道每個變量持有什麼以及它應該保持什麼。

def main(): 
    #set starting variables 
    start_num = int(input('Enter starting number of the range: ')) 
    #check if the numbers entered are positive and that the start is less than the end 
    while (start_num < 1): 
      start_num = int(input('Enter a positive starting number of the range: ')) 
    end_num = int(input('Enter ending number of the range: ')) 
    while (end_num < 1): 
      end_num = int(input('Enter a positive ending number of the range: ')) 

    while (start_num > end_num): 
      start_num = int(input('Enter starting number of the range: ')) 
      end_num = int(input('Enter ending number of the range: ')) 

    cycle_length = 0 
    max_length = 0 
    num_max = 0 
    num = 0 

    while (start_num < end_num): 

兩個start_numend_num永遠不會改變,所以你有一個無限循環,例如而(10 < 100)

  while (num != 1): 

num目前是0,因爲你已經不是分配給任何你把它設置後爲0的幾行前

    if (start_num % 2 == 0): 
          num = start_num/2 

num現在start_num/2,但start_num從未改變

      cycle_length = cycle_length +1 
        else: 
          num = (start_num * 3) + 1 

同樣在這裏

      cycle_length = cycle_length +1  

        if (cycle_length >= max_length): 
          max_length = cycle_length 
          num_max = start_num 

要設置num_maxstart_numstart_num永遠不會改變

    cycle_length = 0 

要重設cycle_num每個週期

  start_num = start_num + 1 
    print(num_max) 
    print(max_length) 

main() 
+0

start_num = start_num + 1 是否每次循環都不更改值? – Rsherrill

+0

'start_num'只改變它爲你生成序列的每個數字的值 – Atsch