Strings
各位大侠,请帮忙看一下附件上的这段代码,为什么总是出现TypeError: steps() missing 1 required positional argument: 'number'
这问题出在哪里?
多谢!
David
#Write a function called "steps" that should return a string
#that, if printed, looks like this:
#
#111
# 222
# 333
#Note that the characters at the beginning of the second and
#third lines must be tabs, not spaces. There should be one
#tab on the second line and two on the third line.
#
#You may only declare ONE string in your function.
#
#Hint: Don't overthink this! We're literally just asking you
#to return one single string that just holds the above text.
#You don't have to build the string dynamically or anything.
#Write your function here!
def steps(number):
tabs =0
nums=1
for i in range(1,number+1):
string = tabs*"\t"+str(nums)*3+'\n'
tabs+=1
nums+=1
print(string)
return ''
#The line below will test your function.
print(steps(3))
print(steps(3))
111
222
333
We found a few things wrong with your code. The first one is shown below, and the rest can be found in full_results.txt in the dropdown in the top left:
We expected steps to return the str
"111
222
333". However, it instead encountered the following error:
TypeError: steps() missing 1 required positional argument: 'number' 第28行直接调用了 step,没有加任何的参数,然而 step 有一个参数,第28行没有传参 tommyyu 发表于 2022-11-7 09:52
第28行直接调用了 step,没有加任何的参数,然而 step 有一个参数,第28行没有传参
Hello Tommy,
谢谢回复!
但是,我把28行的steps()改成steps(3)或者steps(6),还是出现这个TypeError。
还是不知道怎么搞定这个Bug。
David def steps(number):
tabs =0
nums=1
for i in range(1,number+1):
string = tabs*"\t"+str(nums)*3+'\n'
tabs+=1
nums+=1
print(string)
return ''
number=int(input("请输入数字:"))
steps(number) dlin1606 发表于 2022-11-7 10:35
Hello Tommy,
谢谢回复!
{:10_277:} 我这里改完就没有问题了
最后一句的 print ( steps ( 6 ) ) 改为 steps ( 6 ) dragov 发表于 2022-11-7 10:47
最后一句的 print ( steps ( 6 ) ) 改为 steps ( 6 )
没有用,这个好像要在函数里面做什么改变。 本帖最后由 jackz007 于 2022-11-7 11:02 编辑
#Write a function called "steps" that should return a string
#that, if printed, looks like this:
#
#111
# 222
# 333
#Note that the characters at the beginning of the second and
#third lines must be tabs, not spaces. There should be one
#tab on the second line and two on the third line.
别人要求 steps() 返回的是这个字符串而不是 ''
def steps(number): # 函数定义要求调用的时候必须要有一个参数
r = ''
for i in range(number) :
r += '\t' * i + str(i + 1) * 3 + '\n'
return r
print(steps(3)) # 在调用的时候,必须要提供一个参数 tommyyu 发表于 2022-11-7 10:41
我这里改完就没有问题了
在我这个系统里面,这个print(steps())里面不管输入什么数字,还是会出现这个错误,就是说系统已经假设steps()就是这样的,所有应该要在上面函数内做什么改变。 本帖最后由 tommyyu 于 2022-11-7 11:05 编辑
dlin1606 发表于 2022-11-7 10:56
在我这个系统里面,这个print(steps())里面不管输入什么数字,还是会出现这个错误,就是说系统已经假设s ...
#Write a function called "steps" that should return a string
#that, if printed, looks like this:
#
#111
# 222
# 333
#Note that the characters at the beginning of the second and
#third lines must be tabs, not spaces. There should be one
#tab on the second line and two on the third line.
#
#You may only declare ONE string in your function.
#
#Hint: Don't overthink this! We're literally just asking you
#to return one single string that just holds the above text.
#You don't have to build the string dynamically or anything.
#Write your function here!
def steps():
tabs =0
number = 3
nums=1
for i in range(1,number+1):
string = tabs*"\t"+str(nums)*3+'\n'
tabs+=1
nums+=1
print(string)
return ''
#The line below will test your function.
print(steps())
本帖最后由 tommyyu 于 2022-11-7 11:09 编辑
dlin1606 发表于 2022-11-7 10:56
在我这个系统里面,这个print(steps())里面不管输入什么数字,还是会出现这个错误,就是说系统已经假设s ...
或者这个行不行#Write a function called "steps" that should return a string
#that, if printed, looks like this:
#
#111
# 222
# 333
#Note that the characters at the beginning of the second and
#third lines must be tabs, not spaces. There should be one
#tab on the second line and two on the third line.
#
#You may only declare ONE string in your function.
#
#Hint: Don't overthink this! We're literally just asking you
#to return one single string that just holds the above text.
#You don't have to build the string dynamically or anything.
#Write your function here!
def steps(nums):
return '111\n\t222\n\t\t333'
#The line below will test your function.
print(steps(3))
tommyyu 发表于 2022-11-7 11:01
请注意这个要求
#The line below will test your function.
print(steps(3))
print(steps(3))
111
222
333 jackz007 发表于 2022-11-7 10:54
别人要求 steps() 返回的是这个字符串而不是 ''
它的意思是不是只要返回'111\n\t222\n\t\t333'这个字符串就行了?{:10_257:} tommyyu 发表于 2022-11-7 11:08
它的意思是不是只要返回'111\n\t222\n\t\t333'这个字符串就行了?
再翻译一下这个呢:
#Write a function called "steps" that should return a string
#that, if printed, looks like this:
#
#111
# 222
# 333 jackz007 发表于 2022-11-7 11:12
再翻译一下这个呢:
tommyyu 发表于 2022-11-7 11:13
别人要求 steps() 只要返回字符串就好,无需在函数内打印,打印是别人的测试代码要做的事情。 那就应该这样写#Write a function called "steps" that should return a string
#that, if printed, looks like this:
#
#111
# 222
# 333
#Note that the characters at the beginning of the second and
#third lines must be tabs, not spaces. There should be one
#tab on the second line and two on the third line.
#
#You may only declare ONE string in your function.
#
#Hint: Don't overthink this! We're literally just asking you
#to return one single string that just holds the above text.
#You don't have to build the string dynamically or anything.
#Write your function here!
def steps():
tabs =0
number = 3
nums=1
answer = ''
for i in range(1,number+1):
string = tabs*"\t"+str(nums)*3+'\n'
tabs+=1
nums+=1
answer += (string)
return answer
#The line below will test your function.
print(steps())
本帖最后由 jackz007 于 2022-11-7 11:51 编辑
就本题而言,感觉写 Python 代码的能力强并不重要,英语水平高似乎更为关键。 本帖最后由 dlin1606 于 2022-11-7 11:54 编辑
jackz007 发表于 2022-11-7 11:12
再翻译一下这个呢:
def steps():
return '111\n\t222\n\t\t333'
#The line below will test your function.
print(steps())
这个就可以了。 tommyyu 发表于 2022-11-7 11:21
那就应该这样写
def steps():
return '111\n\t222\n\t\t333'
#The line below will test your function.
print(steps())
页:
[1]
2