我的名字是 Seix_Seix,我对我正在构建的 Python 程序有疑问。
问题是,我正在做一个“谜语游戏” (很傻,对吗?),来练习一些基本的 Python 技能。程序的预期流程是你给它一个从 1 到 5 的数字,然后它打开一个包含所有谜题的文件,并在你给出的数字行中打印一个。之后,它会要求您输入一个输入,您在其中输入答案,然后(这是所有崩溃的地方)它将您的答案与另一个文件上的相应行(所有答案都在其中)进行比较。
这是代码,您可以看一下*(它是西班牙语,因为它是我的母语,但在评论中也有翻译和解释)
# -*- 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
我知道当它开始比较答案时会产生这个问题,而且我也知道这可能是因为我写错了...... Sooo,关于如何正确做的任何建议?
提前致谢