为什么我的python没有run_为什么我的python函数没有执行?

I have written a script that is pretty temperamental with indentation so I decided to make functions. I'm pretty new to python and now that I've created these functions nothing works!

def main ():

wiki_scrape()

all_csv()

wiki_set = scraped_set('locations.csv')

country_set = all_set('all.csv')

print wiki_set

I'm just wondering if this is the correct way to call functions from the main() function? I've been debating if an indentation issue is occurring within the called functions, python seems to be very reliant on proper indentations even though it doesn't come up with an error!

解决方案

It sounds like you need to do this:

def main():

wiki_scrape()

all_csv()

wiki_set = scraped_set('locations.csv')

country_set = all_set('all.csv')

print wiki_set

main() # this calls your main function

Even better:

def main():

wiki_scrape()

all_csv()

wiki_set = scraped_set('locations.csv')

country_set = all_set('all.csv')

print wiki_set

if __name__ == '__main__':

main() # this calls your main function

Then run it from the command line like this:

python file_name.py

The built-in variable __name__ is the current contextual namespace. If you run a script from the command line, it will be equivalent to '__main__'. If you run/import the .py file as a module from somewhere else, including from inside the interpreter, the namespace (inside of the context of the module) will be the .py file name, or the package name if it is part of a package. For example:

## my_file.py ##

print('__name__ is {0}'.format(__name__))

if __name__ = '__main__':

print("Hello World!")

If you do this from command line:

python my_file.py

You will get:

__name__ is __main__

Hello World!

If you import it from the interpreter, however, you can see that __name__ is not __main__:

>>> from my_file import *

>>> __name__ is my_file

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值