0
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 * *
 * * * * *
 * * * * *
 * * * * *  
4

1 回答 1

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
于 2013-10-28T00:05:35.817 回答