あなたのコードのほとんどは大丈夫ですが、私が指摘してきたいくつかの問題があります。
- あなたのインデントがオフになっているが。行
cipher += chr(c)
は、forループのものと一致するように字下げする必要があります。
- 関数encypt1()はパラメータを取るべきではありません。メソッド自体にplain1を設定して、そこに宣言できるようにします。
- 小文字だけを扱う場合は、%123( 'z'の値)を実行する必要があり、if節は
c < 97
をチェックする必要があります。あなたがしていることは、印刷可能なasciiをラップしたい場合は問題ありません。
これは与える:
def encrypt1():
plain1 = input('Enter plain text message: ')
cipher = ''
for each in plain1:
c = (ord(each)+3) % 126
if c < 32:
c+=31
cipher += chr(c)
print ("Your encrypted message is:" + cipher)
を使用すると、複数の文字列でこれをテストすることができるようにしたいので、あなたが関数のパラメータとしてplain1渡したいと思うだろう。しかし、パラメータが関数に渡されない場合、ユーザーはplain1を入力できるようにする必要があります。そのために、私はデフォルトのパラメータを提案します。あなたができる
ので
def encrypt1(plain1=""): # if no argument is passed in, plain1 will be ""
if not plain1: # check if plain1 == "" and if so, read input from user
plain1 = input('Enter plain text message: ')
cipher = ''
for each in plain1:
c = (ord(each)+3) % 126
if c < 32:
c+=31
cipher += chr(c)
return cipher # rather than printing the string, return it instead
はこれをテストするには:以下のコメントのコードを見て出力できます
test_strings = ['hello world', 'harambe', 'Name', 'InputString']
for phrase in test_strings:
print ("Your encrypted message is:" + encrypt1(phrase))
:
Your encrypted message is:khoor#zruog
Your encrypted message is:kdudpeh
Your encrypted message is:Qdph
Your encrypted message is:LqsxwVwulqj
は、あなただけのこの行 '暗号をインデントする必要があります+ = chr(c) '。また、引数 'plain1'を関数に渡すことは、' input() 'を使って上書きするので意味がありません。 Pythonのバージョンはこれと関係がありません –
...あなたに質問がありますか? – jonrsharpe