Python if语句始终返回false,即使输入为true

Python if语句始终返回false,即使输入为true,python,if-statement,Python,If Statement,所以我在写一个程序,一种自定义的if语句,检查你的输入是真是假。这是我现在的脚本: v=line[3:] # Remove "if " from the line, so you only have the question and the consequence i=v.split(': ') # Make a list with the first item being the question and the second item being the conseque

所以我在写一个程序,一种自定义的if语句,检查你的输入是真是假。这是我现在的脚本:

v=line[3:] # Remove "if " from the line, so you only have the question and the consequence
i=v.split(': ') # Make a list with the first item being the question and the second item being the consequence.
for r in i:
    if r==i[1]: # Check which item in the list is the question and which is the consequence
        c=r
        question=i[0]
        consequences=c.split(' | ')
    for x in consequences:
        self.consequences+=f'\n{x}' # Create a variable with all of the consequences, each in one line
    Answer=False # reate the Answer variable with the default value False
    def checkVal(question):
        global Answer
        if question:
            b=open('if.BaRT','w+')
            b.write(self.consequences)
            b.close()
            self.run('if.BaRT')
            os.remove('if.BaRT')
        else:
            pass # Check if the question is true or false
    if Answer==True:
        print("True!")
    else:
        print("False!") # Finally check if the question is true or not and if it is, print "True!" and if it's not, print "False!"
我希望这能起作用,但当我输入正确的东西时,例如:_name__==_umain_uu,我的输入如下所示:

if __name__=="__main__": print("Hello!")
这是输出:

False!

如何修复此问题以使其准确打印?

编辑:添加后果

我已经用eval替换了exec,以消除全局变量,但仍然不是很好的编码实践,但是如果这是您需要的。。。。此外,不需要for

PS:变量i、v和c应该有更好的名称

line='if __name__=="__main__": print("Hello!")'
v=line[3:] # Remove "if " from the line, so you only have the question and the consequence
i=v.split(': ') # Make a list with the first item being the question and the second item being the consequence.

c=i[1]
question=i[0]
consequences=c.split(' | ')

_consequences=''
for x in consequences:
   _consequences+=f'\n{x}' 
Answer=eval(f"{question}") # Check if the question is true or false

if Answer==True:
  exec(  _consequences)
  print("True!")
else:
  print("False!") # Finally check if the question is true or not and if it is, print "True!" and if it's not, print "False!"

答案有没有被更新过?使用exec是你应该避免的事情,除非你正在做一些非常具体的强迫你这么做的事情。在这里,这几乎肯定是个坏主意,你应该找到一种更干净的方法来重写你的代码…@AlexWalczak是的,答案在exec函数中更新。如果有什么问题,请告诉我。@Thierrylahuille这就是我想做的,但我不知道怎么做。这就是我问这个问题的原因。然后你应该澄清这段代码应该做什么,并提供一些精心挑选的示例输入数据和预期的输出。非常感谢!这把它修好了。而且,我根本不擅长编写干净的代码。我正在努力。但是,再次感谢你帮助我。