错误,尝试评估我的计算机科学入门练习解决方案时(Python Udacity课程)
我正在学习Python,并试图通过Udacity的“计算机科学入门”(CS101)课程。() 其中一个练习是10排算盘。 我编写了我的解决方案,它在Python IDLE中运行得很好,但estimator不接受我的代码,返回了以下错误消息:错误,尝试评估我的计算机科学入门练习解决方案时(Python Udacity课程),python,python-2.7,Python,Python 2.7,我正在学习Python,并试图通过Udacity的“计算机科学入门”(CS101)课程。() 其中一个练习是10排算盘。 我编写了我的解决方案,它在Python IDLE中运行得很好,但estimator不接受我的代码,返回了以下错误消息: "Incorrect. Your submission did not return the correct result for the input 12345678. The expected output was: '|00000***
"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
10行学校算盘
通过
迈克尔H
说明部分摘自维基百科
在世界各地,abaci已在幼儿园和小学使用
在西方国家,一种类似于俄罗斯算盘的珠子架,但
使用直导线和垂直框架已经很常见(见图)。
帮助学校教授数字系统和算术
|00000******行系数100000000
|00000******行系数100000000
|00000******行系数10000000
|00000******行系数1000000
|00000******行系数100000
|00000******行系数10000
|00000******行系数1000
|00000******行系数100*1
|00000******行系数10*2
|00000******行系数1*3
-----------
总数123
每一行表示不同的行因子,从第一行的x1开始
底部,上升至顶部行的x100000000。
任务:
定义一个采用正整数的过程print_abacus(integer)
并打印一个算盘设置的视觉表示(图像)
给定正整数值。
排名
1星:解决了问题!
双星:6条lst
中必须有10项才能得到正确的结果,但当您这样做时:
while len(lst) <= 10:
lst.insert(0, abacuses["0"])
而len(lst)这是我的代码,希望能帮助您:
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:] + '|')
此my 4启动代码,因为它只需要方法中的3行代码,可能会帮助某些人:
1-)第一行只是创建一个数组,其中每个数字都是一个字符串(数字与数组的索引相同)。例如,数字3是简单的
数字[3]=“00000*****”
2-)第二行此代码仅创建一个缺少零的字符串,例如,对于数字123,它将成为一个十位数的字符数组:“0000000 123”
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)]
手动运行12345678的输出是什么?是的,我自己找到的。愚蠢的错误:(无论如何,谢谢:)没问题,我正要更新我的答案,解释我是如何测试你的代码并发现这一点的,并建议你提高你的调试技能,但这似乎没有必要D
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)]