怎么if __name__ == "__main__":办?
# Threading example
import time, thread
def myfunction(string, sleeptime, lock, *args):
while True:
lock.acquire()
time.sleep(sleeptime)
lock.release()
time.sleep(sleeptime)
if __name__ == "__main__":
lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
Run Code Online (Sandbox Code Playgroud) 每当我开始一个项目时,我都必须想到一个我开始使用的第一个python文件的名称。通常,我只是简单地给它起一个与项目文件夹相同的名称(即,如果文件夹名称是project-x我经常命名的python文件projectX.py)。有没有我应该给我开始使用的第一个python文件的设置名称(例如,main.py也许)?
我想用 python 编写一个脚本,然后从命令行运行它。调用脚本test.py并运行它的命令是:
python3 test.py John Jackson
Run Code Online (Sandbox Code Playgroud)
这是预期的输出:
John Jackson
Run Code Online (Sandbox Code Playgroud)
这是我制作的 python 脚本:
class person:
def __init__(self, first, last):
self.firstname = first
self.lastname = last
def get_first(self):
return self.firstname
def get_last(self):
return self.lastname
import argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument('first')
parser.add_argument('last')
args = parser.parse_args()
c1 = person(args.first, args.last)
print(c1)
Run Code Online (Sandbox Code Playgroud)
但问题是,当我使用上述命令从命令行运行脚本时,它什么也不返回。您知道如何修复它并获得预期的输出吗?
#! /usr/bin/python
def main():
print("boo")
Run Code Online (Sandbox Code Playgroud)
当我尝试在Python 3.3中运行它时,此代码不执行任何操作.没有错误或任何事情.什么是错的
[tim@tim-arch ~]$ gvim script
[tim@tim-arch ~]$ sudo chmod 775 script
[tim@tim-arch ~]$ ./script
[tim@tim-arch ~]$
Run Code Online (Sandbox Code Playgroud) 我编写的程序在从终端执行时没有打印任何内容,因此我尝试运行以下代码
import sys
#!/usr/bin/python
def main(argv):
print "hell0\n"
sys.stdout.flush()
Run Code Online (Sandbox Code Playgroud)
这是终端为什么不打印出hello。main 函数是否还在运行?
我无法弄清楚为什么我不能使用以下代码打印到终端.
#!/usr/bin/env python3
import sys
def main():
sys.stdout.write("Hello")
Run Code Online (Sandbox Code Playgroud)
我正在从终端运行程序,进入找到python文件的目录,使文件可执行并运行
./filename
Run Code Online (Sandbox Code Playgroud)
终端打印什么都没有,只是去换行.如果不使用sys.stdout.write("string"),如何打印到终端?
我正在编写一个带有一些函数的简单Python程序,其中一个main()函数执行其他函数.但是,当我运行下面的代码时,没有输出.有人能告诉我他们是否看到结构中的错误?
def print1():
print("this is also a function")
def print2():
print("this is a function")
def main():
print1()
print2()
Run Code Online (Sandbox Code Playgroud) 我写了一个非常具有缩进气质的脚本,所以我决定制作函数.我是python的新手,现在我已经创建了这些函数,没有任何作用!
def main ():
wiki_scrape()
all_csv()
wiki_set = scraped_set('locations.csv')
country_set = all_set('all.csv')
print wiki_set
Run Code Online (Sandbox Code Playgroud)
我只是想知道这是否是从main()函数调用函数的正确方法?我一直在辩论如果在被调用函数中发生缩进问题,python似乎非常依赖于正确的缩进,即使它没有出现错误!
完整代码 - http://pastebin.com/gJGdHLgr
我的代码有两个问题 - 我不知道如何将我的populateArray函数与我的main函数链接; 我不确定我需要传递什么参数另外,我一直在打开要打开的文件的文件路径 - 路径是正确的,文件存在数据.这是我的代码:
network = []
def populateArray():
file = open('theroute.txt', 'r')
network = []
for line in file:
network.append(line)
print "Network = "
print network
file.close()
def main():
if __name__ == "__main__":
populateArray()
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激!
感谢您的回复 - 我的代码现在看起来像上面,但当我删除def main()时:我收到以下错误:
File "populateArray.py", line 18
if __name__ == "__main__":
^
IndentationError: unindent does not match any outer indentation level
Run Code Online (Sandbox Code Playgroud) 我有这个简单的python程序:
def GetNum (Text):
x = input("Input something: ")
while (x > 0):
x = input("Input something: ")
print x
Run Code Online (Sandbox Code Playgroud)
我想通过终端运行它,但是当我执行命令时:
python ./test.py
Run Code Online (Sandbox Code Playgroud)
或者如果我跑
python test.py
Run Code Online (Sandbox Code Playgroud)
没发生什么事。终端恢复正常,就好像没有执行过任何命令一样。
该文件位于 Documents/Python 下,我在运行命令时位于该目录中。我是否在这里遗漏了一些关于为什么这不起作用的信息?
找不到语法错误或编译错误.为什么这个程序不执行?如果没有编译错误,我无法理解为什么这个程序没有运行.在下面的代码中,逻辑上可能出现什么问题?我可以在此代码中添加哪些内容以使其运行和交互?
def main():
print "Checking platform...\r\r\r"
platform = systemdetails()
def systemdetails():
print "Hello! Welcome to the auto-commander"
print "Please enter the platform specific number to automate."
platforminput = integer(input ("1. Cisco 2. Linux/Unix 3. Juniper 4. VMware vSphere/NSX \n:"))
if platforminput ==1:
platform='cisco_ios'
elif platforminput ==2:
platform='linux'
elif platforminput ==3:
platform='juniper'
elif platforminput ==4:
platform='vmware'
else:
print "Commander has to repeat the question...\n\n"
systemdetails()
return platform
Run Code Online (Sandbox Code Playgroud) 我不知道我的代码有什么问题。它不会执行。什么都没有发生,没有错误发生。我想不通。如果有人能告诉我我做错了什么,请告诉我,我将不胜感激。
class Money (object):
def __init__ (self, euro, cent):
self.euro = euro
self.cent = cent
def __str__ (self):
if self.cent >= 100:
r = self.cent / 100
self.cent = self.cent % 100
self.euro = self.euro + r
return ("%d EUR & %d cents") % (self.euro, self.cent)
else:
return ("%d EUR & %d cents") % (self.euro, self.cent)
def changeCent (self):
#100 c = 1 E
cents = self.euro * 100
self.cent = self.cent + cents
return self.cent
def changeSum (self, euros): …Run Code Online (Sandbox Code Playgroud) python ×11
function ×2
python-3.x ×2
argparse ×1
command-line ×1
filenames ×1
idioms ×1
namespaces ×1
python-2.7 ×1
scripting ×1
ubuntu ×1