从Python 2to3 doc:
input
:转换
input(prompt)
为eval(input(prompt))
我正在尝试使用Python 2几年后学习Python 3.任何人都可以解释为什么工具eval
在调用之前插入input
,以及我是否应该在所有Python 3代码中执行此操作?
我正在尝试使用pip install -r requirements.txt
.来维护依赖项.但是,某些必需的包不直接支持Python 3,但可以使用手动转换2to3
.
在做什么时有办法强制自动pip
运行2to3
这些包pip install -r requirements.txt
吗?
df['direction']
是风向的编号,范围为 1-16。我想把它转换成360-degree system
.
#1
方向是90
,#2
是67.5
,它们顺时针运行。
我可以df['degree'] = 90-(df.direction-1)*22.5
,但这会产生negative value
,你可以看到下面的输出
] 1
但我不知道如何使用conditional
here, for df['degree']<0, then + 360
。我怎样才能做到这一点?
我正在学习 Python 并阅读了有关此错误的博客,但我仍然无法清楚地理解这一点。这是我正在编写的代码片段:
for i in included:
global signs,accounts, regions
global sign_name, acc_name, rg_name
if type == "regions":
regions = i
rg_name = regions['data']['region']
if type == "accounts":
accounts = i
acc_name = accounts['data']['account']
print("Stopping account " + acc_name + " in region " + rg_name)
Run Code Online (Sandbox Code Playgroud)
NameError: 全局名称 'acc_name' 未定义。
我正在使用 Python 2.7 如果有人能帮助我理解 Python 中全局名称和起始的概念,那就太好了。提前致谢。
我有几个函数(a
,b
和c
),我希望它们使用相同的文档字符串。所以我的计划是通过只写一次文档字符串并将其保存到变量来保存行DOCSTRING
。然后我把它放在函数声明下。我在PEP 257中没有找到任何可以解决我的问题的内容......
DOCSTRING = '''
This is a docstring
for functions:
a,
b,
c'''
def a(x, y):
DOCSTRING
# do stuff with x and y
def b(x, y):
DOCSTRING
# do other stuffs with x and y
def c(x, y):
DOCSTRING
# do some more stuffs with x and y
help(a), help(b), help(c)
Run Code Online (Sandbox Code Playgroud)
我实际上认为它可能有效......但我错了,我得到了这个:
Help on function a in module __main__:
a(x, y)
Help on function b in module __main__: …
Run Code Online (Sandbox Code Playgroud) 每当机器人被邀请到服务器时,我都想发送一条消息。然后它应该写一些类似的东西:“你好,这是我的不和谐机器人”
到目前为止,我有这段代码,它不会产生错误,但也不会发送消息。
@bot.event
async def on_server_join(ctx):
for guild in bot.guilds:
for channel in guild.text_channels:
if channel.permissions_for(guild.me).say:
await ctx.message.channel.send('Hello! \n')
break
Run Code Online (Sandbox Code Playgroud) 当我尝试使用pip 安装StringGenerator时,系统出现以下错误提示:
C:\Users\Administrator> pip install StringGenerator
Collecting StringGenerator
Using cached StringGenerator-0.3.0.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\ADMINI~1\AppData\Local\Temp\2\pip-build-mdvrj2cf\StringGenerator\setup.py", line 7, in <module>
long_description = file.read()
File "c:\users\administrator\appdata\local\programs\python\python36-32\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 1264: character maps to <undefined>
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\ADMINI~1\AppData\Local\Temp\2\pip-build-mdvrj2cf\StringGenerator\
Run Code Online (Sandbox Code Playgroud) 为什么不能有多个on_message
活动?
import discord
client = discord.Client()
@client.event
async def on_ready():
print('in on_ready')
@client.event
async def on_message(message):
print("in on_message #1")
@client.event
async def on_message(message):
print("in on_message #2")
@client.event
async def on_message(message):
print("in on_message #3")
client.run("TOKEN")
Run Code Online (Sandbox Code Playgroud)
例如,如果我输入不一致的内容,则总是只有最后on_message
一个被触发。我怎样才能使这三个都正常工作?
我在解决这个问题时遇到了麻烦,如果一个数字n
是回文,则只返回 True 或 False 。
注:哪里有a____
表示哪里有需要填写的空格,有2个空格。
def is_palindrome(n):
x, y = n, 0
f = lambda: ____
while x > 0:
x, y = ____ , f()
return y == n
Run Code Online (Sandbox Code Playgroud)
我在这上面花了大约一个小时。我发现放入x//10
第二个空格将允许函数迭代n
. 然后归结为函数f
。
理想情况下,每次调用它时,它都应该将最后一位数字添加n
到一个新数字中y
。因此,如果n = 235
,while 循环将迭代 3 次,并且每次f()
被调用,它应该将5
, 3
, 和2
,添加到 value y
。
在对我的 Python 模块进行单元测试时,我遇到了一个奇怪的错误:
\n\n\xe2\x85\x94 的构建正常通过,但其中之一无法isclose
从标准math
库导入。
该错误重现如下:
\n\n==================================== ERRORS ====================================\n______________________ ERROR collecting tests/test_yau.py ______________________\nImportError while importing test module \'/home/travis/build/Benjamin-Lee/squiggle/tests/test_yau.py\'.\nHint: make sure your test modules/packages have valid Python names.\nTraceback:\ntests/test_yau.py:5: in <module>\n from math import isclose\nE ImportError: cannot import name \'isclose\'\n!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!\n=========================== 1 error in 0.29 seconds ============================\nThe command "pytest --cov=squiggle" exited with 2.\n
Run Code Online (Sandbox Code Playgroud)\n\n在同一目录中没有指定的文件math.py
(或者在我的包中根本没有)。可能是什么原因造成的?
多次重新启动构建并没有修复这个错误,它只出现在 Python 3.4 中。
\n\n完整日志可在此处访问。
\npython ×10
discord ×2
discord.py ×2
pip ×2
python-2to3 ×2
docstring ×1
iteration ×1
math ×1
palindrome ×1
pandas ×1
pytest ×1
python-3.4 ×1
travis-ci ×1
windows ×1