在尝试评估我从Intro到计算机科学(Python Udacity的课程)的练习解决方案时出错

时间:2016-05-16 12:12:50

标签: python python-2.7

我正在学习Python,并尝试从Udacity通过课程“计算机科学入门”(CS101)。 (https://www.udacity.com/course/intro-to-computer-science--cs101) 其中一个练习是10 Row Abacus。 我编写了我的解决方案,它在我的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(value):

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

P.S。对不起我的英文

3 个答案:

答案 0 :(得分:1)

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

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

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

只需将<=更改为<,这样只有当条件不足时才会添加条目(而少于10个条目会添加条目。)

答案 1 :(得分:0)

这是我的代码,希望对您有所帮助:

selector: 'my-heroes223'

答案 2 :(得分:0)

这是我的4个开始代码,因为它在方法内部仅需要3行代码,并且可能对某人有帮助:

1-)第一行只是创建一个数组,其中每个数字都是一个字符串(数字与数组的索引相同)。例如3是简单的                      数字[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)]