纠正助教

2024-09-30 06:17:54 发布

您现在位置:Python中文网/ 问答频道 /正文

在我尝试添加或更改以下代码(没有成功)以使其正常工作之后,我现在正在尝试从你们那里获得帮助。这是一个python新手的作品,但不幸的是我也是,我不知道如何让它工作。我所用的文献,没有我能用的相同的例子。向你们大家问好

def fun (arg0 , arg ):
fun1 = None
for i in arg :
    if i not in ' aeiouAEIOU ':
        fun1 += 1
        fun2 {i} = 1
        fun3 =+ i
return [ fun1 fun2 fun3 ]

def morefun ( arrg ):
e, o, u = fun ( arrg )
print ('here : ', u)
print ('here , too : ', e)
o = list (o); o. sort ()
for n in o:
    print n, end =' - '
print

par = 'Was it a car or a cat I saw ?'
morefun ( par )

Tags: 代码inforheredefargprintfun
1条回答
网友
1楼 · 发布于 2024-09-30 06:17:54

所以我修复了代码并得到了输出(以我解释它的方式)。 这更是出于好奇

def fun (arg ):
    fun1 = 0
    fun2 = {}
    fun3 = ""
    for i in arg :
        if i not in ' aeiouAEIOU ':
            fun1 += 1
            fun2[i] = fun2.get(i, 0) + 1
            fun3 += i
    return fun1, fun2, fun3

def morefun ( arrg ):
    e, o, u = fun ( arrg )
    print ('here : ', u)
    print ('here , too : ', e)
    o = list (o)
    o. sort ()
    for n in o:
        print(n, end =' - ')
    print

par = 'Was it a car or a cat I saw ?'
morefun ( par )

输出:

here :  Wstcrrctsw?
here , too :  11
? - W - c - r - s - t - w - 

如前所述,代码的作用是: 1.打印不带元音和空格的字符串 2.打印字符串的长度 3.按字母顺序(Ascii)打印所有字母,字母之间带有“-”

一些评论:

  1. 我不知道为什么函数fun()有两个参数 第一个没有用,所以我把它删除了
  2. 当dict被转换成一个列表时,字符串中字母数的信息丢失了,可能是为了按出现的次数排序
  3. 我没有更改变量的名称(即使名称很糟糕)来提供问题的链接
  4. 这是我的解释。仍有一些可能的变化(如第1点和第2点所述)

相关问题 更多 >

    热门问题