2016-03-30 13 views
-5

私はこのプログラムを書いており、私はたくさんの問題を抱えてきました。私はPythonの経験があまりないので、私は文法の誤りがたくさんあると確信しています。私のコードですべてのエラーを見つけるのを助けてください!私はPythonコードで小さなエラーを見つけるのに助けが必要です

import string 
num_ltr = [] 
ltr_num = [] 
num = 1 
for ltr in string.ascii_lowercase: 
    num_ltr[num] = ltr 
    ltr_num[ltr] = num 
    num += 1 

def print_menu(): 
    return '1. Translate a string to numbers' 
    return '2. Translate numbers to a string' 
    return '3. Quit' 

def ltr_to_num(s, ltr_num): 
    for char in s: 
     print ltr_num[char] 
    print 

def num_to_ltr(num_ltr, s): 
    num_list = s.split() 
    sentence = 0 
    for num in num_list: 
     if num.isdigit(): 
     sentence = num_ltr[num] 
    else: 
     sentence += num 

user_choice = 0 
while user_choice != 3: 
    print print_menu() 
    user_choice = raw_input("> ") 
    if user_choice = 1: 
     s = raw_input('Enter a sentence: ') 
     num_to_ltr(s,num_ltr) 
    elif user_choice = 2: 
     s = raw_input('Enter the numbers separated by spaces: ') 
     num_to_ltr(s,num_ltr) 
    elif user_choice != 3: 
     print "I don't recognize that choice." 
print "Goodbye!" 
+3

あなたの行うべきことは、プログラムを実行しようとすることです。最初のエラーは 'IndentationError'です。問題が何であるかを知ることができるはずです: 'sentence = ...'は4つのスペースで字下げする必要があります。あなたがそれをしている間、 'else:'ブロック全体をインデントする必要があります。プログラムを再実行すると、新しい 'IndentationError'が得られます。それも簡単な修正です。問題の多くは、自分で把握できる簡単な修正です。理解できないエラーが発生した場合は、エラーを検索エンジンにコピーします。チャンスは、他の誰かがすでにあなたの問題を抱えていたことです。 – zondo

+0

多くのエラーは、あなたのIDEがあなたに伝える非常に単純です。あなたのプログラムを実行し、あなたに与えられたエラーを読んでください。 – Aaron

+0

さらに、あなたのコードをもっと説明できますか?最初は何を達成しようとしていますか?** print_menu()**の – Aaron

答えて

0

一般的なアドバイス:擬似コードを書く。プログラムを記述し、同時にコードをテストして、エラーを最小限に抑えます。情報は、教師、友人とstackoverflowを集めた。

python 2.7を使用する具体的な理由はありますか?

import string 
num_ltr = [] 
ltr_num = [] 
num = 1 

for ltr in string.ascii_lowercase: 
    num_ltr[num] = ltr 
    ltr_num[ltr] = num 
    num += 1 

def print_menu(): 
    print '1. Translate a string to numbers' 
    print '2. Translate numbers to a string' 
    print'3. Quit' 

def ltr_to_num(s, ltr_num): 
    for char in s: 
     print (ltr_num[char]) 


def num_to_ltr(num_ltr, s): 
    num_list = s.split() 
    sentence = 0 
    for num in num_list: 
     if num.isdigit(): 
      sentence = num_ltr[num] 
     else: 
      sentence += num 

user_choice = 0 
while user_choice != 3: 
    print print_menu() 
    user_choice = raw_input("> ") 
    if user_choice == 1: 
     s = raw_input('Enter a sentence: ') 
     num_to_ltr(s,num_ltr) 
    elif user_choice == 2: 
     s = raw_input('Enter the numbers separated by spaces: ') 
     num_to_ltr(s,num_ltr) 
    elif user_choice != 3: 
     print "I don't recognize that choice." 
print "Goodbye!" 

私はあなたがより多くの助けが必要な場合は私に知らせてください、you'reがトップにそこまでやって何イムわからない、あなたのエラーの一部を修正しました。私は多少役に立ったと思う。

1

私はこの最初の部分で何をしているのか分かりませんが、ここでお手伝いするのは難しいです。あなたがより多くを説明する場合には、修正することが容易になります

for ltr in string.ascii_lowercase: 
    num_ltr[num] = ltr 
    ltr_num[ltr] = num 
    num += 1 
あなたはこれらの行を印刷しようとしている

、ない「戻る」、それらを(あなたが唯一の機能ごとに1つの事を返すことができます)。

def print_menu(): 
    return '1. Translate a string to numbers' 
    return '2. Translate numbers to a string' 
    return '3. Quit' 

代わりにこれを変更し、それを:

while user_choice != 3: 
    print print_menu() 
    user_choice = raw_input("> ") 
    if user_choice == 1: 
     s = raw_input('Enter a sentence: ') 
     num_to_ltr(s,num_ltr) 
    elif user_choice == 2: 
     s = raw_input('Enter the numbers separated by spaces: ') 
     num_to_ltr(s,num_ltr) 
    elif user_choice != 3: 
     print "I don't recognize that choice." 
print "Goodbye!" 
0

あなたはIDE(統合開発環境)を使用している場合:

def print_menu(): 
    print('1. Translate a string to numbers') 
    print('2. Translate numbers to a string') 
    print('3. Quit') 

コードの値を比較するためには、=が割り当てにあり、==ですコードに問題がある場所を強調表示することができます。私はPyCharmを使用します。

ちょうどここでは、whileブロックと行末にあるインデントが間違っていますsentence = num_ltr[num]

また、print_menu関数はreturnを何度も呼び出します。私はあなたが代わりに文字列を一緒に追加したいと思います。おそらく、各行の最後に '\ n'が必要です。

メニューの選択方法を変更する必要があります。入力をintに変換するか、数字の代わりに文字列と比較します(例: 1の代わりに'1'です。

それ以外では、コードを実行してエラーメッセージを読んでみてください。これにより、少しでも作業を進めるための十分な手掛かりが得られるはずです。

関連する問題