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