【发布时间】:2014-07-20 02:33:02
【问题描述】:
我正在尝试使用 Google 的 Python 介绍 (https://developers.google.com/edu/python/exercises/basic) 来开始编程。
根据它给我的这些说明,我编写了以下代码:
# Given an int count of a number of donuts, return a string
# of the form 'Number of donuts: <count>', where <count> is the number
# passed in. However, if the count is 10 or more, then use the word 'many'
# instead of the actual count.
# So donuts(5) returns 'Number of donuts: 5'
# and donuts(23) returns 'Number of donuts: many'
def donuts(count):
if count < 10:
numberofdonuts = count
else:
numberofdonuts = 'many'
print 'Number of donuts:', str(numberofdonuts)
return
如果我只是告诉 Python 打印我编写的代码,它的输出看起来与指令所说的完全一样(例如:“甜甜圈数量:4”)。但是当它通过谷歌提供的测试模块时:
# Provided simple test() function used in main() to print
# what each function returns vs. what it's supposed to return.
def test(got, expected):
if got == expected:
prefix = ' OK '
else:
prefix = ' X '
print '%s got: %s expected: %s' % (prefix, repr(got), repr(expected))
# Provided main() calls the above functions with interesting inputs,
# using test() to check if each result is correct or not.
def main():
print 'donuts'
# Each line calls donuts, compares its result to the expected for that call.
test(donuts(4), 'Number of donuts: 4')
test(donuts(9), 'Number of donuts: 9')
test(donuts(10), 'Number of donuts: many')
test(donuts(99), 'Number of donuts: many')
它不断返回一个“X”并说它得到“无”,如下所示:
donuts
Number of donuts: 4
X got: None expected: 'Number of donuts: 4'
Number of donuts: 9
X got: None expected: 'Number of donuts: 9'
Number of donuts: many
X got: None expected: 'Number of donuts: many'
Number of donuts: many
X got: None expected: 'Number of donuts: many'
我假设 Google 知道如何编写 Python去哪儿?
我想我在这里遗漏了一些非常基本的东西......
【问题讨论】:
标签: python string python-2.7