2016-05-16 5 views
-2

Я изучаю Python и пытаюсь пройти курс «Введение в компьютерную науку» (CS101) из Udacity. (https://www.udacity.com/course/intro-to-computer-science--cs101) Одно из упражнений - 10 строк Abacus. Я написал мое решение, и это работает очень хорошо в моем Python IDLE, но оценщик did't принимает мой код, возвращается сообщение об ошибке:Ошибка при попытке оценить мое решение упражнений от Intro до компьютерных наук (курс Python Udacity)

"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 

Мой код:

Защиту 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 

P.S. Извините за мой английский

+1

Какой результат для 12345678 при запуске вручную? – Kendas

ответ

1

Там должно быть 10 пунктов в lst, чтобы получить правильный результат, но когда вы делаете:

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

Это добавляет дополнительный элемент, когда имеется 10 номеров, это означает, что всегда есть 11 элементов, когда этот цикл завершается.

Просто измените <= в < так, что только добавляет записи, когда не хватает

+0

Да, я действительно нашел его сам. Глупая ошибка :(В любом случае, спасибо :) – vova0808

+0

Нет проблем, я как раз собирался обновить свой ответ, объяснив, как я протестировал ваш код, и нашел это, и предлагаю вам работать с вашими навыками отладки, но, похоже, это не понадобится! : D –

0

Это мой код, хотите, чтобы помочь вам (в то время как есть меньше, чем 10 записей добавить один.):

def print_abacus(value): 
image = '|00000*****' 
str_value = str(value) 

lines = len(str_value) 
n = 10 - lines 

for i in range(n): 
    print(image + " |") 

for a in range(0, lines): 
    num = int(str_value[a]) 
    if num == 0: 
     print(image + " |") 
    else: 
     print(image[0:11-num] + ' ' + image[-num:] + '|') 
Смежные вопросы