错误,当试图评估我的练习解决方案从计算机科学入门(Python Udacity 的课程)时



我正在学习Python,并试图通过Udacity的课程"计算机科学导论"(CS101)。(https://www.udacity.com/course/intro-to-computer-science--cs101)练习之一是十排算盘。我写了我的解决方案,它在我的Python IDLE中运行得很好,但估计器不接受我的代码,返回了以下错误消息:

   "Incorrect. Your submission did not return the correct result for the input 12345678. The expected output was:
    '|00000*****   |n|00000*****   |n|00000****   *|n|00000***   **|n|00000**   ***|n|00000*   ****|n|00000   *****|n|0000   0*****|n|000   00*****|n|00   000*****|'
    Your submission passed 1 out of 3 test cases"

我不知道问题出在哪里如果有人能告诉我哪里是我的错,我会感激的!

演习描述:

     10-row School abacus
                         by
                      Michael H
       Description partially extracted from from wikipedia 
  Around the world, abaci have been used in pre-schools and elementary
 In Western countries, a bead frame similar to the Russian abacus but
 with straight wires and a vertical frame has been common (see image).
 Helps schools as an aid in teaching the numeral system and arithmetic
         |00000*****   |     row factor 1000000000
         |00000*****   |     row factor 100000000
         |00000*****   |     row factor 10000000 
         |00000*****   |     row factor 1000000
         |00000*****   |     row factor 100000
         |00000*****   |     row factor 10000
         |00000*****   |     row factor 1000
         |00000****   *|     row factor 100     * 1
         |00000***   **|     row factor 10      * 2
         |00000**   ***|     row factor 1       * 3
                                        -----------    
                             Sum                123 
 Each row represents a different row factor, starting with x1 at the
 bottom, ascending up to x1000000000 at the top row.     

 TASK:
 Define a procedure print_abacus(integer) that takes a positive integer
 and prints a visual representation (image) of an abacus setup for a 
 given positive integer value.
 Ranking
 1 STAR: solved the problem!
 2 STARS: 6 < lines <= 9
 3 STARS: 3 < lines <= 6
 4 STARS: 0 < lines <= 3

我的代码:

def print_abacus(值):

abacuses = {
    "0" : "|00000*****   |",
    "1" : "|00000****   *|",
    "2" : "|00000***   **|",
    "3" : "|00000**   ***|",
    "4" : "|00000*   ****|",
    "5" : "|00000   *****|",
    "6" : "|0000   0*****|",
    "7" : "|000   00*****|",
    "8" : "|00   000*****|",
    "9" : "|0   0000*****|"}

lst = []

s = str(value)
for i in s:    
    for key in abacuses: 
        if i == key: 
            lst.append(abacuses[key])

while len(lst) <= 10:
    lst.insert(0, abacuses["0"])

for abacus in lst:
    print abacus

第页。S.对不起我的英语

lst中必须有10个项目才能获得正确的结果,但当您这样做时:

while len(lst) <= 10:
    lst.insert(0, abacuses["0"])

当有10个数字时,它会添加一个额外的条目,这意味着当这个循环结束时,总是有11个项目。

只需将<=更改为<,使其仅在条目不足时添加条目(当条目少于10个时添加一个条目)。

这是我的4启动代码,因为它只需要方法中的3行代码,可能会对某人有所帮助:

1-)第一行只是创建一个数组,其中每个数字都是一个字符串(数字与数组的索引相同)。例如,数字3只是numbers[3]="|00000***|"。

2-)这段代码的第二行只是创建了一个缺少零的字符串,例如对于数字123,它变成了一个由十位字符组成的数组:"0000000123"。

3-)在第三行中,每个字符被再次转换为数字,并用作数字数组的索引,然后逐个打印。

def print_abacus(value):
   #line code 1
   numbers= ["|00000*****   |",
            "|00000****   *|",
            "|00000***   **|",
            "|00000**   ***|",
            "|00000*   ****|",
            "|00000   *****|",
            "|0000   0*****|",
            "|000   00*****|",
            "|00   000*****|",
            "|0   0000*****|",]
   #line code 2 and 3        
   for n in ("0"*(10-len(str(value)))+str(value)):
       print numbers[int(n)]

最新更新