Python 如何从main方法自动启动程序
这是我的密码Python 如何从main方法自动启动程序,python,Python,这是我的密码 def main(): # This code reads in data.txt and loads it into an array # Array will be used to add friends, remove and list # when we quit, we'll overwrite original friends.txt with # contents print"Welcome to the program"
def main():
# This code reads in data.txt and loads it into an array
# Array will be used to add friends, remove and list
# when we quit, we'll overwrite original friends.txt with
# contents
print"Welcome to the program"
print "Enter the correct number"
print "Hockey fan 1, basketball fan 2, cricket fan 3"
choice = input("Select an option")
while choice!=3:
if choice==1:
addString = raw_input("Who is your favorite player??")
print "I love Kessel"
elif choice==2:
remInt = raw_input("Do you think that the Cavaliers will continue ther loosing ways?")
print "I think they can beat the Clippers"
else:
"You must choose a Number (1,2 or 3)"
print "Cricket is a great sport"
choice = input("Select an option")
inFile = open('data.txt','r')
listNumbers = []
for numbers in inFile:
listNumbers.append(numbers)
print numbers
inFile.close()
if __name__ == "__main__":
main() # will call the 'main' function only when you execute this from the command line.
加:
添加到脚本(一直缩进到左侧;不是作为main()
函数的一部分)。添加:
if __name__ == "__main__":
main()
在文件末尾,您应该尝试以下操作
if __name__ == "__main__":
main()
完成后,您应该按照以下方式从命令行调用您的程序(假设您在Linux/Mac上)
python
如果你给出你得到的确切错误,这也会很有帮助。
还要确保所有缩进都是正确的。我试过了,但程序不会运行。它显示语法错误。请您将它添加到我的代码末尾,然后运行它,看看它是否适合您。我手头没有python解释器。错误信息是什么?@liam:你的代码对我来说很好。应该注意的是,这个问题没有意义,因为它已经被一些“聪明”的人纠正了。嘘。问答。
if __name__ == "__main__":
main()
python <your_prog>