table =
[['*', '*', '*', '*', '*'],
['*', '*', '*', '*', '*'],
['*', '*', '*', '*', '*'],
['*', '*', '*', '*', '*'],
['*', '*', '*', '*', '*']]
def print_table():
for row in range(5):
for col in range(5):
print table[row][col],
print ""
def clear_table():
for row in range(5):
for col in range(5):
table[row][col] = "*"
def table_has_letter(letter):
for letter in clear_table:
if letter in clear_table:
print true
else:
print false
必须使用模块(%)
def create_table(secret):
clear_table()
secret = secret.upper()
secret.replace(" ","")
secret.split(",")
for col in range(5):
row = 1
if len(secret) % 5:
row += 1
print [len(secret) % 5]
print secret[col]
create_table("mathematics")
print_table()
这就是预期的结果:
P L A Y F
A I R * *
* * * * *
* * * * *
* * * * *
答案 0 :(得分:0)
您可能想要修改:
def print_table():
for row in range(5):
for col in range(5):
print table[row][col],
print # Added so it print new lines
你的create_table应该是这样的:
def create_table(secret):
clear_table()
secret = secret.upper()
secret = secret.replace(" ",'')
secret.split(",")
row = 0
col = 0
for i in range(len(secret)):
if i != 0 and i % 5 == 0:
row += 1
col = 0
table[row][col] = secret[i]
col += 1
<强>测试强>
create_table("Hi world")
print_table()
>>>
H I W O R
L D * * *
* * * * *
* * * * *
* * * * *
create_table("play fair")
print_table()
>>>
P L A Y F
A I R * *
* * * * *
* * * * *
* * * * *
修改强>
要删除重复的字母,请使用以下方法:
def removeDup(mystring):
newStr = ""
for ch in mystring:
if ch not in newStr:
newStr = newStr + ch
return newStr
并将其添加到您的create_table
方法。
def create_table_without_rep(secret):
clear_table()
secret = secret.upper()
secret = secret.replace(" ",'')
secret.split(",")
row = 0
col = 0
secret = removeDup(secret) # Add this line
for i in range(len(secret)):
if i != 0 and i % 5 == 0:
row += 1
col = 0
table[row][col] = secret[i]
col += 1