简体   繁体   English

为什么我的功能不起作用?

[英]Why dont my functions work?

This is python 3, this code basically checks if a word is the same when read backwards. 这是python 3,此代码基本上检查单词向后读取时是否相同。 When i execute this through Visual Studio, nothing happens, and I get the prompt to press any key to continue... 当我通过Visual Studio执行此操作时,什么也没有发生,并且提示我按任意键继续...

if "__name__" == "__main__":
    StartProgram() 

def StartProgram():
    Input = AskForDataSimple()
    print(CheckIfPalindrome(Input))

def AskForDataSimple():
    print("Please input the line to test.")
    In = input()
    return In

def CheckIfPalindrome(x):
    if x[::-1] == x:
        return True
    else:
        return False

Please note that this simpler version actually works: 请注意,这个简单的版本实际上可以工作:

x = input()

if x[::-1] == x:
    print(True)
else:
    print(False)
if "__name__" == "__main__":

Change this to 更改为

if __name__ == "__main__":

__name__ is a variable containing name of this module. __name__是包含此模块名称的变量。 You need these line so that your main logic would be used only if this file is executed directly, not when imported as a module by another code. 您需要这些行,以便仅在直接执行此文件时才使用您的主要逻辑,而在由另一个代码作为模块导入时则不会使用。

Still it won't work, because you need to define the function you call before these lines: move these lines to the end of the file. 仍然不起作用,因为您需要在这些行之前定义要调用的函数:将这些行移到文件末尾。

Also, this 还有这个

def CheckIfPalindrome(x):
    if x[::-1] == x:
        return True
    else:
        return False

can be replaced with 可以替换为

def CheckIfPalindrome(x):
    return x[::-1] == x

Move main function to bottom of file and try it 将主要功能移至文件底部并尝试

if __name__ == "__main__":
      StartProgram() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM