我正在看Don Marco的一篇文章,其中涉及在python中制作Pascal的三角形。我想更好地理解代码,所以我尝试使用它并尝试让它接受用户输入。这是我使用的代码:
def triangle(rows):
row_ans= raw_input('how many rows would you like')
row_ans =int(row_ans)
for rownum in range (rows):
newValue=1
PrintingList = [newValue]
for iteration in range (rownum):
newValue = newValue * ( rownum-iteration ) * 1 / ( iteration + 1 )
PrintingList.append(int(newValue))
print(PrintingList)
print()
triangle(row_ans)
它没有要求任何用户输入,我收到了这个错误:
Traceback (most recent call last):
File "/Users/centralcity/Desktop/Computer Science!/Pascal's triangle", line 13, in
<module>
triangle(row_ans)
File "/Users/centralcity/Desktop/Computer Science!/Pascal's triangle", line 3, in
triangle
for rownum in range (rows):
TypeError: range() integer end argument expected, got str.
请记住,我太蟒蛇了。提前致谢。
答案 0 :(得分:1)
您将错误的参数传递给range()
。在最外面的for
循环中尝试这个:
range(row_ans)
另请注意,rows
参数未被使用,请将其从函数声明中删除,然后只需调用函数:
triangle()