1

Pythonでパスカルの三角形を作成することに関するDon Marcoの投稿を見ていました。コードをよりよく理解したかったので、試してみて、ユーザー入力を取得しようとしました。これは私が使用したコードです:

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.

私はかなり新しいPythonでもあることに注意してください。前もって感謝します。

4

1 に答える 1