它应该计算行中的行数和字符数。
我无法向显示功能添加任何更多变量,因为教授说使用2。
如果更改zXX = zXX + 1,我已经正确计算了行中的字符数 到zXX = w 但是,如果我这样做,它将不计算行数,如果有人可以帮助我,我会非常感激。
目前我有:
def display(x, y): y = str(y) varx = str(len(y)) vary = y + "#" + x + "#" + varx return vary.rjust(3) def main(): script = '''Grandson|Cough, cough, cough. Cough, cough, cough. {Grandson is on the bed, playing video game.} Mother|{Enters.} Hi, honey. Grandson|Hi, Mom. Mother|{Kisses son and feels his forehead.} You feeling any better? Grandson|A little bit. Mother|Guess what? Grandson|What? Mother|Your Grandfather's here. {Opens curtains.} Grandson|Mom, can't you tell him I'm sick? Mother|You're sick? That's why he's here. Grandson|He'll pinch my cheek. I hate that. Mother|Maybe he won't. Grandfather|{Entering with a flourish.} Heyyyy!! How's the sickie? Heh? {Pinches boy's cheek. Boy looks at mother accusingly.} Mother|I think I'll leave you two pals alone. {Exits.} Grandfather|I brought you a special present. Grandson|What is it? Grandfather|Open it up. Grandson|{Opens the package. Disappointed.} A book? Grandfather|That's right. When I was your age, television was called books. And this is a special book. It was the book my father used to read to me when I was sick, {takes book} and I used to read it to your father. And today I'm gonna read it to you. Grandson|Has it got any sports in it? Grandfather|Are you kidding? Fencing, fighting, torture, revenge, giants, monsters, chases, escapes, true love, miracles... Grandson|Doesn't sound too bad. I'll try to stay awake. {Turns off TV.} Grandfather|Oh, well, thank you very much, very nice of you. Your vote of confidence is overwhelming. All right. {Puts glasses on.} The Princess Bride, by S. Morgenstern. Chapter One. Buttercup was raised on a small farm in the country of Florin.''' zX = script.split('\n') #print(zX) zXX = 0 for w in zX: #print(zX[zXX:(zXX + 1)]) zXXXX = w.split('|') #print(zXXXX) zXXX = zXXXX[0].upper() + " " + zXXXX[1] #print(zXXX) zXX = zXX + 1 print(display(zXXX, zXX)) main()
输出结果为:
1#GRANDSON咳嗽,咳嗽,咳嗽。咳嗽,咳嗽,咳嗽。 {孙子躺在床上,玩电子游戏。}#12#MOTHER {进来。}嗨,亲爱的。#1
3#GRANDSON嗨,妈妈。#1
4#MOTHER {亲吻儿子并感受到他的额头。}你感觉好点了吗?#1
5#GRANDSON一点点。#1
6#MOTHER猜猜是什么?#17#GRANDSON什么?#1
8#母亲你的祖父在这里。 {打开窗帘。}#1
9#GRANDSON妈妈,你不能告诉他我生病了吗?#1 10#母亲你生病了吗?这就是他在这里的原因。#2 11#GRANDSON他会捏我的脸颊。我讨厌那个。#2 12#母亲也许他不会。#2 13#GRANDFATHER {蓬勃发展。}嘿嘿!!病人怎么样?嘿? {捏男孩的脸颊。男孩责备地看着母亲。}#2 14#母亲我想我会留下两个好朋友。 {退出。}#2 15#GRANDFATHER我带给你一个特别的礼物。#216#GRANDSON这是什么?#2
17#GRANDFATHER打开它。#2
18#GRANDSON {打开包裹。失望。}一本书?#2
19#GRANDFATHER那是对的。当我年纪的时候,电视被称为书籍。这是一本特别的书。这是我父亲生病时给我读的那本书,{拿书'而且我曾经把它读给你父亲。今天我要读给你看。#2
20#GRANDSON它有没有运动?#2 21#GRANDFATHER你在开玩笑吗?击剑,战斗,折磨,复仇,巨人,怪物,追逐,逃脱,真爱,奇迹...#2 22#GRANDSON听起来不太糟糕。我会尽力保持清醒。 {关闭电视。}#2 23#GRANDFATHER哦,非常感谢,非常感谢你。你的信任投票势不可挡。行。 {戴上眼镜。}公主新娘,由S. Morgenstern。第一章。毛茛是在弗罗林的一个小农场长大的。#2答案 0 :(得分:0)
一旦我超越了格式和变量名称,并且你没有在'script'中包含换行符,我很确定你遇到的问题是:
varx = str(len(y))
而不是:
varx = str(len(x))
虽然这是基于我对你提供的内容的解释