私は現在タクシーの番号を見つけることを目的とした小さなスクリプトに取り組んでいます。小さな問題は1つしかなく、forループは変数xをインクリメントせず、永遠にループしています。for loop stuck and interating(Python 3)
n = int(10)
counter = int(0)
m = int(m)
x = int(1)
y = int(n**(1/3))
cheatsheet = []
while counter in range(0,m):
for x in range(1,y):
if x**3 + y**3 < n:
print('less than')
print(x,y,n)
continue
elif x**3 + y**3 > n:
print('greater than')
y -= 1
continue
elif x > y:
if n in cheatsheet:
print('counting')
counter = counter+1
#checking if n occurs in the list, if so it will add 1 to the counter so that the program will know when to stop
n = n + 1
y = int(n**(1/3))
x = 1
print('break1')
#resetting values and bumping n so that it will continue the quest to find 'em all
break
else:
if x and y == 1:
#this is an error correction for low n values, i mean really low it might be redundant by now
n = n + 1
y = int(n**(1/3))
x = 1
print('break2')
break
cheatsheet.append((n,x,y))
print(cheatsheet)
これは、ターミナルウィンドウで、次の結果が得られます:私は自分でプロセスを殺し image
なお、プログラムはしなかった私は、以下のコードの関連部分を貼り付けます。 あなたが分かるように、スクリプトはちょうどループし、「より小さい」とx、y、nの値を出力します。
お願いします。
EDIT:変数mはユーザによって与えられますが、このコードの桁には含まれません。
あなたのforループは正常に動作していますが、あなたのwhileループはキャンセルされていないと思います。 continue文はforループからプログラムを取得しますが、whileループのカウンタは決して更新されません。 – Skirrebattie
私がアップロードした画像を見ると、x = 1からx = 2にバンプすることはありません。したがって、それは固まっています。 – Uninvolved
範囲(1,2)は、その1だけを反復することを意味します。意味:xの範囲(1,3)に を入力した場合: print(x) あなたは を取得します。 2 – Skirrebattie