2017-11-06 10 views
1

次のコードは、リストから約100までの数値シーケンスを出力します。かなりの量のシーケンスが100を超えて印刷されます。ボタンに100を加えた数字だけを印刷する方法を知りたいのですが。私は運がないリストに結果を印刷しようとしました。私は結果をフィルタリングするためにifとelse文を入れてみましたが、運はありませんでした。リスト内包を調べましたが、whileループを使用しないので、forループで同じ結果を得る方法がわかりません。私がオンラインで見つけることができる唯一の情報は、whileループの使い方と数字のリストの印刷方法に関する基本的なレッスンです。私は印刷された数字のリストをどのようにソートするかについて何も見つけることができませんでした。Python 3でwhileループの出力を並べ替える

import itertools 

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

for i in list1: 
    a = 0 
    num1 = 2 
    num2 = i 
    seq = ([a]) 
    it = itertools.cycle((num1,num2)) 
    while a < 100: 
     a += next(it) 
     print(a, end = " ") 
     seq.append(a) 
    print()  
    print("Here are the numbers", num1, "&", num2, "added together in a sequence") 
    print() 

出力:ここ

はコードで私が欲しいもの

2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26 27 29 30 32 33 35 36 38 39 41 42 44 45 47 48 50 51 53 54 56 57 59 60 62 63 65 66 68 69 71 72 74 75 77 78 80 81 83 84 86 87 89 90 92 93 95 96 98 99 101 
Here are the numbers 2 & 1 added together in a sequence 

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 
Here are the numbers 2 & 2 added together in a sequence 

2 5 7 10 12 15 17 20 22 25 27 30 32 35 37 40 42 45 47 50 52 55 57 60 62 65 67 70 72 75 77 80 82 85 87 90 92 95 97 100 
Here are the numbers 2 & 3 added together in a sequence 

2 6 8 12 14 18 20 24 26 30 32 36 38 42 44 48 50 54 56 60 62 66 68 72 74 78 80 84 86 90 92 96 98 102 
Here are the numbers 2 & 4 added together in a sequence 

2 7 9 14 16 21 23 28 30 35 37 42 44 49 51 56 58 63 65 70 72 77 79 84 86 91 93 98 100 
Here are the numbers 2 & 5 added together in a sequence 

2 8 10 16 18 24 26 32 34 40 42 48 50 56 58 64 66 72 74 80 82 88 90 96 98 104 
Here are the numbers 2 & 6 added together in a sequence 

2 9 11 18 20 27 29 36 38 45 47 54 56 63 65 72 74 81 83 90 92 99 101 
Here are the numbers 2 & 7 added together in a sequence 

2 10 12 20 22 30 32 40 42 50 52 60 62 70 72 80 82 90 92 100 
Here are the numbers 2 & 8 added together in a sequence 

2 11 13 22 24 33 35 44 46 55 57 66 68 77 79 88 90 99 101 
Here are the numbers 2 & 9 added together in a sequence 

2 12 14 24 26 36 38 48 50 60 62 72 74 84 86 96 98 108 
Here are the numbers 2 & 10 added together in a sequence 

です:

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 
Here are the numbers 2 & 2 added together in a sequence 

2 5 7 10 12 15 17 20 22 25 27 30 32 35 37 40 42 45 47 50 52 55 57 60 62 65 67 70 72 75 77 80 82 85 87 90 92 95 97 100 
Here are the numbers 2 & 3 added together in a sequence 

2 7 9 14 16 21 23 28 30 35 37 42 44 49 51 56 58 63 65 70 72 77 79 84 86 91 93 98 100 
Here are the numbers 2 & 5 added together in a sequence 

2 10 12 20 22 30 32 40 42 50 52 60 62 70 72 80 82 90 92 100 
Here are the numbers 2 & 8 added together in a sequence 

この上の任意およびすべてのヘルプは大歓迎されます。

答えて

2

いったん完了すると、シーケンスの追加が最大100まで追加されるので、そのポイントより前に印刷を開始することはできません。これは仕事をする必要があります:

import itertools 

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

for i in list1: 
    a = 0 
    num1 = 2 
    num2 = i 
    seq = ([a]) 
    it = itertools.cycle((num1,num2)) 
    while a < 100: 
     a += next(it) 
     seq.append(a) 
    if seq[-1] == 100: # -1 as an index gets the last entry in a list 
     print(" ".join([str(val) for val in seq])) 
     print("Here are the numbers", num1, "&", num2, "added together in a sequence") 
     print() 
+0

私はその変更を実行すると何も起こりません。 –

+0

申し訳ありませんが、 'join'コマンドの行にバグがありました。文字列のリストのみを受け入れることを忘れていたので、intのリストの内容を最初に文字列に変換する必要があります。 – Arne

+0

これで動作します。ありがとうございました。 –

関連する問題