我正在创建一个python脚本,打印出'99瓶啤酒'的整首歌曲,但是相反.我唯一无法逆转的是数字,整数,而不是字符串.
这是我的完整脚本,
def reverse(str):
return str[::-1]
def plural(word, b):
if b != 1:
return word + 's'
else:
return word
def line(b, ending):
print b or reverse('No more'), plural(reverse('bottle'), b), reverse(ending)
for i in range(99, 0, -1):
line(i, "of beer on the wall")
line(i, "of beer"
print reverse("Take one down, pass it around")
line(i-1, "of beer on the wall \n")
Run Code Online (Sandbox Code Playgroud)
我理解我的反向函数将字符串作为参数,但是我不知道如何接受整数,或者如何在脚本中反转整数.
doctest很难控制。我遇到了这样的问题
功能
from collections import namedtuple
Match = namedtuple('Match', ['token_string', 'normalised_token',
'brand_name', 'brand_id',
'score'])
def make_match(tokens, normalised, brand, score):
"""
Examples:
>>> make_match('Jack Jones','JackJones',('Jack Jones','X023'),0.6)==Match('Jack Jones','JackJones','Jack Jones','X023',0.6)
True
>>> make_match('Jack Jones','JackJones',('Jack Jones','X023'),0.6)==('Jack Jones','JackJones','Jack Jones','X023',0.6)
True
>>> match=make_match('Jack Jones','JackJones',('Jack Jones','X023'),0.6)
>>> match.token_string=='Jack Jones'
True
"""
return Match(token_string=tokens,
normalised_token=normalised,
brand_name=brand[0],
brand_id=brand[1],
score=score)
Run Code Online (Sandbox Code Playgroud)
但是有一个错误
Failed example:
make_match('Jack Jones','JackJones',('Jack Jones','X023'),0.6)==Match('Jack Jones','JackJones','Jack Jones','X023',0.6)
Expected:
True
Got:
True
Run Code Online (Sandbox Code Playgroud)
1个项目有故障:
难道不是完全匹配了吗?非常感谢利用率4中的1。make_match 测试失败 1个失败。
使用Python NumPy计算矩阵列的总和:
import numpy
from StringIO import StringIO
fileName = 'test2.csv'
myFile = open(fileName,'r')
print "Reading data from '%s' ..." % fileName
data = myFile.read()
myFile.close()
data = numpy.genfromtxt(StringIO(data), delimiter=',', usecols=(0,1,2))
print "Calculating ..."
print data[:,2]
sumA1 = data[:,2].sum
print "shape =", data.shape
print "sumA1 =", str(sumA1)
print "ok"
Run Code Online (Sandbox Code Playgroud)
然后内容test2.csv:
12,13,14,17
1,2,3,4
12,13,14,17
1,2,3,4
12,13,14,17
1,2,3,4
12,13,14,17
1,2,3,4
12,13,14,17
Run Code Online (Sandbox Code Playgroud)
我得到了这个输出
Reading data from 'test2.csv' ...
Calculating ...
[ 14. 3. 14. 3. 14. 3. 14. 3. 14.]
shape …Run Code Online (Sandbox Code Playgroud) 我有声明类的功能
>>> def a():
... class A:
... print "a"
...
Run Code Online (Sandbox Code Playgroud)
当我调用函数时,为什么要打印a?
>>> a()
a
Run Code Online (Sandbox Code Playgroud) 我想在我的剧本依赖项中添加一个角色,但要基于条件。
- name: Get all install pyenv versions
command: '{{ pyenv_root }}bin/pyenv versions'
register: available_versions
tags:
- get_pyenv_versions
environment:
PYENV_ROOT: "{{ pyenv_root }}"
dependencies:
- { role: pyenv, python_versions: ["{{ mypython_version }}"], when: "mypython_version not in available_versions.stdout" }
Run Code Online (Sandbox Code Playgroud)
我想做的是,我想检查所有可用pyenv版本,如果mypython_version不可用,那么只有我想调用pyenv角色,否则我不想调用它。
它给了我语法错误
ERROR! Syntax Error while loading YAML.
The error appears to have been in '/root/ansible_playbooks/roles/mydeployment/meta/main.yaml': line 9, column 1, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line …Run Code Online (Sandbox Code Playgroud) 我刚刚开始学习Python,并试图调用我在另一个函数中定义的一个函数.解释器返回错误:NameError:未定义全局名称'isPrime'
这是我的代码的样子:
def split (value):
x = 2
halfOne, halfTwo = 0, 0
if isPrime(value) == True:
print (value)
return
else:
while x < value:
if value % x == 0:
halfOne = value / x
halfTwo = x
split(halfOne)
split(halfTwo)
x += 1
def is_prime(value):
# some code
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助!