如何运行google开发者Python课程第一个练习(string1.py)的解决方案? [重复]

问题描述 投票:0回答:2

我尝试使用命令提示符打印以下 python 函数的解决方案,但出现错误。我不确定我给出的命令是否错误或者代码本身有任何错误。你能检查一下吗?

# A. donuts
# 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:
    return 'Number of dounts:' + str(count)
  else:
    return 'Number of donuts: many'

我在命令提示符下给出了以下命令

C:\>PythonCourse\google-python-exercises\basic\string1 donuts('10')

出现以下错误:

 File "C:\PythonCourse\google-python-exercises\basic\string1.py", line 77
    print '%s got: %s expected: %s' % (prefix, repr(got), repr(expected))
SyntaxError: invalid syntax

我也尝试运行解决方案文件,但出现相同的错误。我是 python 新手,任何帮助将不胜感激。我正在使用 python 3.8。 如果我需要提供任何其他详细信息,请告诉我。

python command-prompt
2个回答
2
投票

您的解决方案似乎对我有用,但检查器似乎希望您将甜甜圈的数量作为数字传递,在这种情况下您应该使用格式化字符串:

if count < 10:
    return f'Number of donuts: {count}'
else:
    return 'Number of donuts: many'

第二个返回语句中的“甜甜圈”一词也有拼写错误。


-1
投票

在Python3中,你必须在打印函数中使用括号。像这样:

print ('Hello World')
© www.soinside.com 2019 - 2024. All rights reserved.