鱼C论坛

 找回密码
 立即注册
查看: 4077|回复: 22

Strings

[复制链接]
发表于 2022-11-7 09:38:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
各位大侠,

请帮忙看一下附件上的这段代码,为什么总是出现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

[Executed at: Sun Nov 6 17:36:12 PST 2022]

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'
String.3.png
String.4.png
String.5.png
String.2.png
String.1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-11-7 09:52:07 | 显示全部楼层
第28行直接调用了 step,没有加任何的参数,然而 step 有一个参数,第28行没有传参
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-7 10:35:44 | 显示全部楼层
tommyyu 发表于 2022-11-7 09:52
第28行直接调用了 step,没有加任何的参数,然而 step 有一个参数,第28行没有传参

Hello Tommy,

谢谢回复!
但是,我把28行的steps()改成steps(3)或者steps(6),还是出现这个TypeError。
还是不知道怎么搞定这个Bug。

David
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-7 10:40:09 | 显示全部楼层
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)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-7 10:41:59 | 显示全部楼层
dlin1606 发表于 2022-11-7 10:35
Hello Tommy,

谢谢回复!

我这里改完就没有问题了
屏幕截图 2022-11-07 104116.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-7 10:47:30 | 显示全部楼层


最后一句的 print ( steps ( 6 ) ) 改为 steps ( 6 )
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-7 10:53:59 | 显示全部楼层
dragov 发表于 2022-11-7 10:47
最后一句的 print ( steps ( 6 ) ) 改为 steps ( 6 )

没有用,这个好像要在函数里面做什么改变。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-7 10:54:52 | 显示全部楼层
本帖最后由 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))           # 在调用的时候,必须要提供一个参数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-7 10:56:00 | 显示全部楼层
tommyyu 发表于 2022-11-7 10:41
我这里改完就没有问题了

在我这个系统里面,这个print(steps())里面不管输入什么数字,还是会出现这个错误,就是说系统已经假设steps()就是这样的,所有应该要在上面函数内做什么改变。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-7 11:01:17 | 显示全部楼层
本帖最后由 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())
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-7 11:06:11 | 显示全部楼层
本帖最后由 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))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-7 11:08:07 | 显示全部楼层

        请注意这个要求
#The line below will test your function.
print(steps(3))

print(steps(3))
111

        222

                333
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-7 11:08:36 | 显示全部楼层
jackz007 发表于 2022-11-7 10:54
别人要求 steps() 返回的是这个字符串而不是 ''

它的意思是不是只要返回'111\n\t222\n\t\t333'这个字符串就行了?
屏幕截图 2022-11-07 110731.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-7 11:12:30 | 显示全部楼层
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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-7 11:13:23 | 显示全部楼层
jackz007 发表于 2022-11-7 11:12
再翻译一下这个呢:


屏幕截图 2022-11-07 111258.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-7 11:20:31 | 显示全部楼层

        别人要求 steps() 只要返回字符串就好,无需在函数内打印,打印是别人的测试代码要做的事情。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-7 11:21:52 | 显示全部楼层
那就应该这样写
#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())
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-7 11:50:43 | 显示全部楼层
本帖最后由 jackz007 于 2022-11-7 11:51 编辑

          就本题而言,感觉写 Python 代码的能力强并不重要,英语水平高似乎更为关键。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-7 11:53:09 | 显示全部楼层
本帖最后由 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())

这个就可以了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-7 11:55:31 | 显示全部楼层
tommyyu 发表于 2022-11-7 11:21
那就应该这样写

def steps():
    return '111\n\t222\n\t\t333'
#The line below will test your function.
print(steps())
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-25 19:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表