我有以下代碼,出于教學目的,我想從這里簡化為使用迭代。我希望學生能夠看到任何重復(例如模式)并使用 for 或 while 回圈來實作這一點。
解決這個問題的最佳方法是什么?
https://trinket.io/python/5e56cf6a5c
def matrix():
print("---The Matrix---")
#create a 1d array of 7 stars
matrix1=[
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"]
]
#user enters a number
number=int(input("Enter number:"))
#you are always finding the remainder on each row to place the X
remainder=number%7
#an 'X' is placed in the position of the number
#remainder-1 because we start at index 0
if number<1:
matrix1[0][0]="X"
elif number<=7:
matrix1[0][remainder-1]="X"
elif number>7 and number<15:
matrix1[1][remainder-1]="X"
elif number>14 and number<22:
matrix1[2][remainder-1]="X"
elif number>21 and number<29:
matrix1[3][remainder-1]="X"
elif number>28 and number<36:
matrix1[4][remainder-1]="X"
elif number>35 and number<43:
matrix1[5][remainder-1]="X"
elif number>42 and number<50:
matrix1[6][remainder-1]="X"
#the updated matrix is printed.
print(matrix1)
matrix()
根本不使用 iteraton,建議這樣做:
def matrix():
print("---The Matrix---")
# create a 2d array of 7x7 stars
matrix1 = [["*" for _ in range(7)] for _ in range(7)]
number = int(input("Enter number: "))
matrix1[number // 7][number % 7] = "X"
# print the matrix
print('\n'.join(''.join(row) for row in matrix1))
matrix()
我無法理解以下內容:
比如說'14'。matrix1[number // 7][number % 7] = "X" number//7 = 2 and number%7 = 0. 這應該把 X 放在第 2 行和位置 0?
uj5u.com熱心網友回復:
這不使用任何回圈,但也許這種簡化符合您的要求。
def matrix():
print("---The Matrix---")
#create a 1d array of 7 stars
matrix1=[
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"]
]
#user enters a number
number=int(input("Enter number:"))
#you are always finding the remainder on each row to place the X
q,r = divmod(number-1,7)
matrix1[q][r] = 'X'
#the updated matrix is printed.
print(matrix1)
matrix()
此外,這是列印該矩陣的更漂亮的方法。
for row in matrix1:
print(*row)
在 Python 2 中:
for row in matrix1:
print(' '.join(row))
這是Python 3 版本的飾品鏈接。
示例輸出:
---The Matrix---
Enter number:8
* * * * * * *
X * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
uj5u.com熱心網友回復:
您不需要這樣做remainder-1,因為remainder=number%7總是回傳 0 到 6 之間的值。
0%7 = 0
1%7 = 1
...
6%7 = 6
7%7 = 0
這解決了您的根本問題。
uj5u.com熱心網友回復:
您可以使用這樣的回圈:
def matrix():
print("---The Matrix---")
#create a 1d array of 7 stars
matrix1=[
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"],
["*","*","*","*","*","*","*"]
]
#user enters a number
number= int(float(input("Enter number:")))
#you are always finding the remainder on each row to place the X
#an 'X' is placed in the position of the number
#remainder-1 because we start at index 0
for i in range(1,len(matrix1) 1):
for j in range(1,len(matrix1) 1):
if i * j == number:
matrix1[i-1][j-1] = "X"
#the updated matrix is printed.
print(matrix1)
matrix()
uj5u.com熱心網友回復:
正如您所說,它將 14 放入第 2 行第 0 列。您的實作有錯誤。例如,0 和 1 具有相同的輸出。因此,通常如果您使用零索引,建議的代碼是完美的。
當我們為您的實作提供 14 時,由于 el-if 陳述句,它假定它在第 1 行。matrix1[1][remainder-1]變成matrix1[1][-1]然后它把 X 放到行的最后一個元素。python 的 -1 功能顯示您的實作是正確的。但事實并非如此。
如果您使用零索引,建議的實作是正確的。如果你說它應該從 1 開始,那么你的回圈實作也是錯誤的。
uj5u.com熱心網友回復:
如果使用 2D 陣列不是關鍵部分,那么更好的選擇是對單個串列進行簡單替換,并使用其他函式來設定輸出樣式。這里我們使用textwrap將文本拆分為 7 個字符長的行:
import textwrap
def matrix():
print("---The Matrix---")
# Create a list of '*', 49 items long
matrix1= ["*"] * 49
#user enters a number
number=int(input("Enter number:"))
# Update the list element in that position
matrix1[number] = "X"
#the updated matrix is printed.
print('\n'.join(textwrap.wrap(''.join(matrix1), 7)))
matrix()
誠然,這也意味著不涉及回圈或迭代,這可能會破壞練習的重點......
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/384577.html
下一篇:無法在回圈中執行setter
