2016-04-05 6 views
-1

私の目標は、pythonのダイヤモンドを印刷することです。ここに私の未遂のコードは次のとおりです。ダイヤモンドのアスタリスクを使用しているときにPythonを使用してループ

height=int(input("Please enter height : ")) 
row=1 
#top half of diamond 
while(row<height+1): 
    spaces=0 
    while (spaces<(height-row)): 
      spaces+=1 
      print ("",end='') 

    j=2*row-1 
    while (j>0): 
      j=j-1 
      print("*",end='') 
    row+=1 
    print() 

row=height-1 
#bottom half of diamond 
while (row>0): 
    spaces=0 
    while(spaces<height-row): 
      spaces+=1 
      print("",end='') 
    j=2*row-1 
    while(j>0): 
      print("*",end='') 
      j=j-1 
    row=row-1 
    print() 

このコードは次のようにダイヤモンドの右側のみを印刷するようだ:

それは左側を出力するように、私は、コードを修正するにはどうすればよい
Please enter height : 5 
* 
*** 
***** 
******* 
********* 
******* 
***** 
*** 
* 

ダイヤモンド?

答えて

2

print ("",end='')あなたの意図はスペースを印刷することでしたが、""はスペースではありません。空の文字列です。代わりに、試してみてくださいprint (" ",end='')

+0

恐ろしい!本当にありがとう! –

関連する問題