2016-10-31 5 views
0

私は、長い除算の問題の合計を計算するコードを記述しようとしています。これは私がキャリー用に書いた関数です。私は最初のwhileループがすべて機能していないという問題を信じています。もしそうなら、それはなぜですか?これはPython 3.5にあります。私のプログラムは最初のwhileループを通っていないのはなぜですか?

carries = 0 
sum1 = 0 
countx = str(x) 
county = str(y) 
countx1 = len(countx) 
county1 = len(county) 
countx2 = int(countx1) 
county2 = int(county1) 
carry_over = 0 
count = countx1 
index = 1 
index1 = 1 
carrieslist = [0,0,0,0,0,0,0,0,0,0] 
while (index1 <= county1): 
    sum3 = county[-index1] 
    while (index <= count): 
     sum2 = countx[0-index] 
     sum4 = int(sum2) 
     sum5 = int(sum3) 
     sum1 = sum4*sum5+carry_over 
     sum6 = str(sum1) 
     index = index+1 
     if (sum1 > 9): 
      carries += 1 
      carry_over = int(sum6[0]) 
      if carry_over > 0: 
       if carry_over == 1: 
        carrieslist[1] += 1 
       if carry_over == 2: 
        carrieslist[2] += 1 
       if carry_over == 3: 
        carrieslist[3] += 1 
       if carry_over == 4: 
        carrieslist[4] += 1 
       if carry_over == 5: 
        carrieslist[5] += 1 
       if carry_over == 6: 
        carrieslist[6] += 1 
       if carry_over == 7: 
        carrieslist[7] += 1 
       if carry_over == 8: 
        carrieslist[8] += 1 
       if carry_over == 9: 
        carrieslist[9] += 1 
     else: 
      carry_over = 0 
    index1 = index1 + 1 
print(" ") 
print("Carries:",carries) 
print(" ") 
if sum(carrieslist) > 0: 
    print("Carry digit --> Frequency") 
countlist = 0 
indexlist = 0 
while (countlist < len(carrieslist)): 
    if (carrieslist[indexlist] > 0): 
     print(indexlist,"-->",carrieslist[indexlist]) 
     indexlist += 1 
     countlist += 1 
    else: 
     indexlist += 1 
     countlist += 1 
+0

county [-index1]という行の負のインデックスにアクセスしようとしていますか?あなたがここで直面している実際の問題は何ですか?ところで、xとyは何ですか?あなたは実際に何をしたいですか? –

+0

xとyは、私が長い乗算を乗算している2つの数字です(例:9929と129)。そして、なぜ9×9929のキャリーをカウントするのか不思議ですが、 2と1の129です。うまくいけば、私はいつも必要に応じてプロンプトを投稿することができます –

+0

申し訳ありませんがあなたの説明は私には分かりません。あなたは、あなたの記事の中で「長い分裂問題の総運搬回数を数えます」という意味を説明できますか? –

答えて

1

この問題を特定しました。次のように外側のwhileループの中で変数index = 1を初期化するだけです。

while (index1 <= county1): 
    sum3 = county[-index1] 
    index = 1 
    while (index <= count): 
     sum2 = countx[0-index] 
     sum4 = int(sum2) 
     ................ 
     ................ 

コードが読みやすくなるようにコードスタイルを改善してください。問題を見つけるのに多くの時間がかかりました。なぜなら、それぞれのステートメントで書いたことを理解するのは難しいからです。

+0

ありがとう、私はスクランブルアップコードのためにお詫び申し上げます –

関連する問題