Discuss / Python / 作业

作业

Topic source

有为牛犊

#1 Created at ... [Delete] [Delete and Lock User]

请教各位,如果我想跳过输入第二个Hello,Lisa!,只输出第一个和第三个,应该怎么做?

下面的代码是自己的尝试,但是只能输入第一个,第二个和第三个都没有输入,请问是什么问题

L = ['Bart', 'Lisa', 'Adam']i = 0while i <= len(L)-1:    if L[i] == "Lisa":        continue    print("Hello,%s" % L[i])    i = i + 1

有为牛犊

#2 Created at ... [Delete] [Delete and Lock User]

不好意思,发错地方,应该发在循环那一章

强小易

#3 Created at ... [Delete] [Delete and Lock User]

if L[i] == "Lisa":

        continue

这里就阻断了代码往后执行了,你的i=i+1始终在L[i]=="Lisa"的时候被阻断了,导致死循环

应该改成这样

L = ['Bart', 'Lisa', 'Adam']

i = 0

while i <= len(L) - 1:

    if L[i] == "Lisa":

        i = i + 1

        continue

    print("Hello,%s" % L[i])

    i = i + 1

虽然不优雅不知道Py有没有i++之类的操作如果有可以改成

L = ['Bart', 'Lisa', 'Adam']

i = 0

while i <= len(L) - 1:

    if L[i++] == "Lisa":

        i = i + 1

        continue

    print("Hello,%s" % L[i])

L = ['Bart', 'Lisa', 'Adam']

i = -1 

while i < 2:

    i = i+1

    if i == 1:

        continue

    print(L[i])


  • 1

Reply