【问题标题】:Python comparing input to line in file?Python将输入与文件中的行进行比较?
【发布时间】:2012-10-05 13:59:57
【问题描述】:

我的名字是 Seix_Seix,我对我正在构建的 Python 程序有疑问。

问题是,我正在做一个“谜语游戏”(很傻,对吗?),来练习一些基本的 Python 技能。程序的预期流程是你给它一个从 1 到 5 的数字,然后它打开一个包含所有谜语的文件,并在你给出的数字行中打印一个。 之后,它会要求您输入一个输入,您在其中输入答案,然后 (这是所有崩溃的地方) 它将您的答案与 another 上的相应行进行比较文件(所有答案都在其中)

这是代码,你可以看一下*(它是西班牙语,因为它是我的母语,但它在 cmets 中也有翻译和解释)

# -*- 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,任何建议怎么做才对?

提前致谢

【问题讨论】:

    标签: python file input compare


    【解决方案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)
    

    根据要求,关于您的代码的其他一些 cmets:

    总的来说,还可以。您可以优化以下几点:

    您没有关闭已打开的文件。当您只是阅读它们时这不是问题,但是一旦您开始写入文件就会导致问题。最好快点习惯。最好的方法是使用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&lt;tab&gt;t.txt 的文件,因为 \t 将被解释为制表符。

    另外,花点时间研究一下PEP-8, the Python style guide。它将帮助您编写更具可读性的代码。

    由于您使用的是 Python 2,因此您可以删除 print (ques[guess-1]) 中的括号(或切换到 Python 3,因为 Unicode,我还是会推荐它!另外,在 Python 3 中,raw_input() 最终被重命名为 @ 987654335@)。

    然后,我认为您需要从答案字符串中去除尾随换行符,否则它们将无法正确比较(另外,删除不必要的括号):

    if a == ans[guess-1].rstrip("\n"): 
    

    【讨论】:

    • 亲爱的妈妈……它确实奏效了!我现在感觉真的,真的很蠢......¬但说真的,谢谢你非常。对于骰子部分,我很抱歉,我非常专注于解决比较问题,以至于我完全忘记了它......哦!还有一件事......你能给我你对代码的看法吗?我在这里是一个完整的 n00b,所以我可以使用我能得到的所有反馈......再次感谢 Seix_Seix
    • @Seix_Seix:当然,我已经编辑了我的答案。我希望你会和我一样喜欢 StackOverflow。您可能还想花点时间阅读FAQ,这将帮助您更好地开始(不会冒着惹恼这里一些更脾气暴躁的老前辈的风险):)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    相关资源
    最近更新 更多