1

私の名前はSeix_Seixですが、作成しているPythonのプログラムに疑問があります。

問題は、 Pythonの基本的なスキルを練習するために、「なぞなぞ」(ばかげているでしょ?)をしているということです。プログラムの意図するフローは、1から5までの数字を指定してから、すべてのなぞなぞが格納されているファイルを開き、指定した数字の行に1つを出力することです。その後、回答を入力する入力を求められ、(これはすべてが崩れた場所です)別のファイルの対応する行(すべての回答がある場所)と回答を比較します。

これがあなたがそれを見ることができるようにコードです*(それは私の母国語なのでスペイン語ですが、コメントに翻訳と説明もあります)

# -*- coding: cp1252 -*-

f = open ("C:\Users\Public\oo.txt", "r") #This is where all the riddles are stored, each one in a separate line
g = open ("C:\Users\Public\ee.txt", "r") #This is where the answers to the riddles are, each one in the same line as its riddle
ques=f.readlines()
ans=g.readlines()

print "¡Juguemos a las adivinanzas!" #"Lets play a riddle game!"
guess = int(raw_input("Escoge un número entre 1 y 5. O puedes tirar los dados(0) ")) #"Choose a number from 1 to 5, or you can roll the dice (0)" #This is the numerical input, in which you choose the riddle

if guess==0:
    import random
    raw_input(random.randrange(1, 5))

print (ques[guess-1]) #Here, it prints the line corresponding to the number you gave, minus 1 (because the first line is 0, the second one is 1 and so on)
a=input("¿Sabes qué es?") #"Do you know the answer?" #Here, you are supposed to type the answer to the riddle.

while True:
    if a==(ans[guess-1]): #And here, it is supposed to compare the answer you gave with the corresponding line on the answer file (ee.txt). 
        print "ok" #If you are correct it congratulates you, and breaks the loop.
        break
    else:
        print "no" #If you are wrong, it repeats its question over and over again

それで、私はプログラムを実行します。答えを入力しなければならない瞬間まで、しばらくの間はすべて問題ありません。そこに、私が何を入れても、それが正しいか間違っていても、それは私に次のエラーを与えます:

Traceback (most recent call last):
  File "C:\Users\[User]\Desktop\lol.py", line 16, in <module>
    a=input("¿Sabes qué es?") #"Do you know the answer?" #Here, you are supposed to type the answer to the riddle.
  File "<string>", line 1, in <module>
NameError: name 'aguacate' is not defined #It is the correct answer BTW

答えを比較し始めるとこの問題が発生することを私は知っています、そしてそれはおそらく私がそれを間違って書いたためであることも知っています...すっごく、それを正しく行う方法について何かアドバイスはありますか

前もって感謝します

4

1 に答える 1

1

raw_input()の代わりにを使用する必要があります。そうしないinput()と、Python は入力された文字列を評価しようとしaguacateます。 は Python が認識できる式ではないため、見つかった例外をスローします。

また、「サイコロを振る」ルーチンが機能しません (入力0してみて、何が起こるかを確認してください)。そうあるべき

if guess == 0:
    # import random should be at the start of the script
    guess = random.randrange(1,6)

要求に応じて、コードに関するその他のコメント:

一般的に、それはまったく問題ありません。最適化できる小さなことがいくつかあります。

開いたファイルを閉じていません。それらを読み取るだけであれば問題ありませんが、ファイルを書き始めると問題が発生します。早く慣れたほうがいいです。これを行う最善の方法は、withステートメント ブロックを使用することです。プログラムの実行中に例外が発生した場合でも、ファイルを自動的に閉じます。

with open(r"C:\Users\Public\oo.txt") as f, open(r"C:\Users\Public\ee.txt") as g:
    ques = f.readlines()
    ans = g.readlines()

生の文字列を使用したことに注意してください (文字列にバックスラッシュがある場合は重要です)。ファイルに という名前を付けた場合、がタブ文字として解釈されたため、tt.txtという名前のファイルが検索されたため、バージョンは失敗していました。Public<tab>t.txt\t

また、 Python スタイル ガイド である PEP-8についても学習してください。より読みやすいコードを書くのに役立ちます。

Python 2 を使用しているので、括弧を削除できますprint (ques[guess-1])(または、Python 3 に切り替えることができます。これは、Unicode であるため、とにかくお勧めします! また、Python 3 では、raw_input()最終的に に名前が変更されましたinput())。

次に、回答文字列から末尾の改行文字を取り除く必要があると思います。そうしないと、正しく比較されません (また、不要な括弧を削除します)。

if a == ans[guess-1].rstrip("\n"): 
于 2012-10-05T19:10:47.983 に答える