代码片段输出与错误分析:字符串操作练习.pdfVIP

  • 1
  • 0
  • 约7.74千字
  • 约 8页
  • 2026-01-27 发布于北京
  • 举报

代码片段输出与错误分析:字符串操作练习.pdf

在下表的每个代码片段旁边,写出打印的内容。如果代码会导致错误,写ERROR并给出简要解释。

_

tweet1=#uoftcs得主SteveCooktweet2=想要便宜的零

食?去@cssuBA2283

Code输出或错误

print(Turingintweet1)True

print(inintweet1andinintweet2)True

print(tweet1[1]+tweet2[‑3])u2

print(tweet1[tweet2[‑1]])ERROR:stringindicesmustbeintegers

print(tweet2[tweet2.find(@):])@cssuofficeinBA2283

问题2.[2marks]

通过添加参数来完成示例函数调用,以获得所示的返回值。(对于每个示例调用,有多个正确,其中

任何一个都可以获得满分。)

defmystery(L):(listofstr)‑str#包含一个以z开头和结尾的字符串

的列表mystery([z])zz#包含三个分别以a和c、d和f、g和

i开头和结尾的字符串的列表。mystery([abc,def,ghi])acdfgi

result=对于L中的每个项:result=

result+项[0]+项[‑1]

返回结果

Question1.[5marks]

Besideeachcodefragmentinthetablebelow,writewhatisprinted.Ifthecodewouldcauseanerror,

writeERRORandgiveabriefexnation.

tweet1=#uoft_csTuringawardwinnerSteveCook

tweet2=Wantcheapsnacks?Visit@cssuofficeinBA2283

CodeOutputorCauseofError

print(Turingintweet1)True

print(inintweet1andinintweet2)True

print(tweet1[1]+tweet2[-3])u2

print(tweet1[tweet2[-1]])ERROR:stringindicesmustbeintegers

print(tweet2[tweet2.find(@):])@cssuofficeinBA2283

Question2.[2marks]

Completetheexamplefunctioncallsbyaddingargumentsthatresultinthereturnvaluesshown.(For

eachexamplecall,thereareseveralcorrectanswers,andprovidinganyoneofthemwillearnfullmarks.)

defmystery(L):

(listofstr)-str

#alistcontainingonestringthatst

文档评论(0)

1亿VIP精品文档

相关文档