2016-10-05 5 views
0

のPythonで行うにはクールなことのためにグーグルを検索するとき、私はちょうど、パイソンの基礎を学んだが、このPDFファイルを見つけました:Binary_Image (1/0 *に、スペースを変換する)このPDFファイルは、変更」出力を数え、改行を7文字ごとにする方法は?

の言うチャレンジ部を有しあなたのプログラムは6文字の表示幅を持つようにすることができます。 "position"と呼ばれる新しい変数を作成し、ユーザが入力するすべての文字に対して1を加え、 の位置が6になるたびに "改行"

私は挑戦を理解することは何私は理解していないが、私はこのコード

を試してみましたので、「位置」の変数を使用する方法である改行を作る img_outその後、すべての6つの文字

をカウントする変数を作っていますと誰も私を助けることができる場合python path/to/file.py

Enter your b&w bitmap image :11111101101101 
Traceback (most recent call last): 
    File "C:\Users\saber\Desktop\testing.py", l 
    if len(img_out) >= "7": 
TypeError: unorderable types: int() >= str() 

を使用してcmd /kでコードをコンパイル

#get a binary number from the user 
img_in = input("Enter your b&w bitmap image :") 
#initially, there is no output 
img_out = "" 
#loop through each character in the binary input 
for character in img_in: 


    #add a star(*) to the output if a 1 is found 
    if character == "1": 
     img_out = img_out + "*" 
    #otherwise, add a space 
    else: 
     img_out = img_out + " " 

     #count the img_out 
     if len(img_out) >= "7": 
      img_out = img_out + "\n" 
     else: 
      img_out = img_out 
#print the image to the screen 
print(img_out) 

事前に おかげで素晴らしいものだだこのchalangeを解決する方法

PS:私はWindows上のPython 3.5.2を使用

答えて

0

変更:

if len(img_out) >= "7": 
     img_out = img_out + "\n" 

へ:

if len(img_out) >= 7: 
     img_out = img_out + "\n" 

更新

#get a binary number from the user 
img_in = input("Enter your b&w bitmap image :") 

#initially, there is no output 
img_out = "" 

#loop through each character in the binary input 
for character in img_in: 

    #add a star(*) to the output if a 1 is found 
    if character == "1": 
     img_out = img_out + "*" 
    #otherwise, add a space 
    elif character == '0': 
     img_out = img_out + " " 
    if len(img_out.replace('\n', '')) % 7 == 0: 
     img_out = img_out + "\n" 

#print the image to the screen 
print(img_out) 

あなたのコードでは、文字列が7より長いかどうかをテストしていましたが、それが1回以上チェックされたときには行が途切れることになりました。私のコードでは、文字列の位置が7で割り切れているかどうかをテストしています。その場合、7番目の文字が書き込まれるたびにそれが破れます。私はまた、 '\ n'は文字であり、私がこれをしているときに数えられないので、置換を使用しています。

モジュラスhereの詳細を確認して、hereを置き換えることができます。

+0

ありがとうございました!私は答えを受け入れる:)感謝 –

+0

ありがとうPro-Fun :) –

+0

それは私を助けるが、私は別の小さな問題を持っています:元として。私は111101101110111を入力したとき:これは私を与える必要があります **** ** *** ** * 行1:4つ星スペース2つ星 ライン2:スペース3つ星スペース2つ星 ライン3:星 が、最初の7 1/0を与え、最初の7の後の各1/0は単一行になります –

関連する問題