私は初心者のプログラマーであり、第1言語としてのPythonを学んでいます。 練習のために、私は完了したsudokuパズルを生成するプログラムを作った、 うまくいけばGUIでスドクゲームの一環として後で使用される。しかし今のところ、それは何もしません。そして、それは私がそれが画面上に何も印刷されていないときに実行されることを意味します。私はすべてが正しく書式設定されていることを確認するためにチェックし、私の理解のために私の構文はすべて正しいです。私の注意を逃した論理的なエラーはありますか?いずれにせよ、私は何かが私のwhileループが壊れるのを防ぐと思う。また、私のプログラムはおそらく不必要に長くなっているかもしれませんが、私はまだプログラムを学び始めているので、私は非常に限られたツールキットしか扱うことができませんでした。ここでなぜ私のpythonプログラムのwhileループは終了しないのですか?
は、その全体が私のプログラムです:あなたは解決できないボードの状態に入るならば、それは後戻りすることができ、あなたのアルゴリズムでは何もないので
# Sudoku puzzle maker
# Practice game that makes an unfinished sudoku puzzle for the player to solve
import random
# The readout string stores the assigned numbers given to each square
# in the puzzle.
readout = ""
# The "c" strings store the numbers contained in each of the columns.
# The program will check these strings to make sure that only one of each
# number from 1 to 9 is in each column.
# The same is true for the "r" (row) and "b" (box) strings.
c1 = ""
c2 = ""
c3 = ""
c4 = ""
c5 = ""
c6 = ""
c7 = ""
c8 = ""
c9 = ""
r1 = ""
r2 = ""
r3 = ""
r4 = ""
r5 = ""
r6 = ""
r7 = ""
r8 = ""
r9 = ""
b1 = ""
b2 = ""
b3 = ""
b4 = ""
b5 = ""
b6 = ""
b7 = ""
b8 = ""
b9 = ""
# The "square" strings store the location of each square as a 3-digit code,
# the first digit is the column, the second the row, and the third the box.
# For example, the fourth square is in c4 (column 4), r1 (row 1),
# and b2 (3x3 box 2), so its corresponding string is "412".
square_1 = "111"
square_2 = "211"
square_3 = "311"
square_4 = "412"
square_5 = "512"
square_6 = "612"
square_7 = "713"
square_8 = "813"
square_9 = "913"
square_10 = "121"
square_11 = "221"
square_12 = "321"
square_13 = "422"
square_14 = "522"
square_15 = "622"
square_16 = "723"
square_17 = "823"
square_18 = "923"
square_19 = "131"
square_20 = "231"
square_21 = "331"
square_22 = "432"
square_23 = "532"
square_24 = "632"
square_25 = "733"
square_26 = "833"
square_27 = "933"
square_28 = "144"
square_29 = "244"
square_30 = "344"
square_31 = "445"
square_32 = "545"
square_33 = "645"
square_34 = "746"
square_35 = "846"
square_36 = "946"
square_37 = "154"
square_38 = "254"
square_39 = "354"
square_40 = "455"
square_41 = "555"
square_42 = "655"
square_43 = "756"
square_44 = "856"
square_45 = "956"
square_46 = "164"
square_47 = "264"
square_48 = "364"
square_49 = "465"
square_50 = "565"
square_51 = "665"
square_52 = "766"
square_53 = "866"
square_54 = "966"
square_55 = "177"
square_56 = "277"
square_57 = "377"
square_58 = "478"
square_59 = "578"
square_60 = "678"
square_61 = "779"
square_62 = "879"
square_63 = "979"
square_64 = "187"
square_65 = "287"
square_66 = "387"
square_67 = "488"
square_68 = "588"
square_69 = "688"
square_70 = "789"
square_71 = "889"
square_72 = "989"
square_73 = "197"
square_74 = "297"
square_75 = "397"
square_76 = "498"
square_77 = "598"
square_78 = "698"
square_79 = "799"
square_80 = "899"
square_81 = "999"
# The "master_list" is a tuple that stores all of the "square" strings in order
# of their appearance. I realize now that I could have just had it store the
# strings directly, but I'm too lazy to go back and change it.
master_list = (square_1, square_2, square_3, square_4, square_5, square_6, square_7, square_8, square_9,\
square_10, square_11, square_12, square_13, square_14, square_15, square_16, square_17, square_18,\
square_19, square_20, square_21, square_22, square_23, square_24, square_25, square_26, square_27,\
square_28, square_29, square_30, square_31, square_32, square_33, square_34, square_35, square_36,\
square_37, square_38, square_39, square_40, square_41, square_42, square_43, square_44, square_45,\
square_46, square_47, square_48, square_49, square_50, square_51, square_52, square_53, square_54,\
square_55, square_56, square_57, square_58, square_59, square_60, square_61, square_62, square_63,\
square_64, square_65, square_66, square_67, square_68, square_69, square_70, square_71, square_72,\
square_73, square_74, square_75, square_76, square_77, square_78, square_79, square_80, square_81)
# This for loop, for each square, picks a random number, converts it from an
# integer into a string, and checks to see if it is already in the same column,
# row, or box. It checks this by deciding which "c" "r" and "b" string
# to look into based on the 3-digit code in each "square" string, and if
# it finds that the number it picked is already in the same c, r, or b, it
# tries again with a new random number. If the number fits, it is added to
# the proper "c", "r", and "b" strings for future reference, and added to the
# "readout" string.
for square in master_list:
while True:
number = str(random.randint)
# Here the program finds which column the square is in, and checks that column.
if square[0] == "1":
column = c1
if number in c1:
continue
elif square[0] == "2":
column = c2
if number in c2:
continue
elif square[0] == "3":
column = c3
if number in c3:
continue
elif square[0] == "4":
column = c4
if number in c4:
continue
elif square[0] == "5":
column = c5
if number in c5:
continue
elif square[0] == "6":
column = c6
if number in c6:
continue
elif square[0] == "7":
column = c7
if number in c7:
continue
elif square[0] == "8":
column = c8
if number in c8:
continue
elif square[0] == "9":
column = c9
if number in c9:
continue
#Here the program finds which row it is in and checks the row.
if square[1] == "1":
row = r1
if number in r1:
continue
elif square[1] == "2":
row = r2
if number in r2:
continue
elif square[1] == "3":
row = r3
if number in r3:
continue
elif square[1] == "4":
row = r4
if number in r4:
continue
elif square[1] == "5":
row = r5
if number in r5:
continue
elif square[1] == "6":
row = r6
if number in r6:
continue
elif square[1] == "7":
row = r7
if number in r7:
continue
elif square[1] == "8":
row = r8
if number in r8:
continue
elif square[1] == "9":
row = r9
if number in r9:
continue
#Here it finds which box it is in and checks the box.
if square[2] == "1":
box = b1
if number in b1:
continue
elif square[2] == "2":
box = b2
if number in b2:
continue
elif square[2] == "3":
box = b3
if number in b3:
continue
elif square[2] == "4":
box = b4
if number in b4:
continue
elif square[2] == "5":
box = b5
if number in b5:
continue
elif square[2] == "6":
box = b6
if number in b6:
continue
elif square[2] == "7":
box = b7
if number in b7:
continue
elif square[2] == "8":
box = b8
if number in b8:
continue
elif square[2] == "9":
box = b9
if number in b9:
continue
# If a random number has gotten this far, it means it has passed inspection.
# Now the program concatenates the number to the correct "c", "r" and "b"
# strings for future reference.
if column == c1:
c1 += number
elif column == c2:
c2 += number
elif column == c3:
c3 += number
elif column == c4:
c4 += number
elif column == c5:
c5 += number
elif column == c6:
c6 += number
elif column == c7:
c7 += number
elif column == c8:
c8 += number
elif column == c9:
c9 += number
if row == r1:
r1 += number
elif row == r2:
r2 += number
elif row == r3:
r3 += number
elif row == r4:
r4 += number
elif row == r5:
r5 += number
elif row == r6:
r6 += number
elif row == r7:
r7 += number
elif row == r8:
r8 += number
elif row == r9:
r9 += number
if box == b1:
b1 += number
elif box == b2:
b2 += number
elif box == b3:
b3 += number
elif box == b4:
b4 += number
elif box == b5:
b5 += number
elif box == b6:
b6 += number
elif box == b7:
b7 += number
elif box == b8:
b8 += number
elif box == b9:
b9 += number
# Now the number is added to the readout and the while loop breaks, moving
# the for loop on to the next square.
readout += number
break
print(readout)
input("\n\nyay it worked.")
問題がうまくいかないときは、問題の簡単な例を作成するのが偉大なデバッグ手法です。この場合、2x2数独のパズルを作成することができます。 –
私はこのコードを読んでも耐えることができませんが、* way *の変数が多すぎます。リストや索引付けなどのデータ構造を使用する方が、名前の数字だけが異なる数十の変数を持つよりもずっと優れています。 – Blckknght
'str(random.randint)'は、文字列化された整数ではないと思いますが、 ''> '"を返します。これを修正するには、開始番号と停止番号を入力します。 'str(random.randint(0,9))'。 –