木水盗人 发表于 2021-3-5 22:21:17

关于列表和循环

请各位大佬帮忙检查下循环那里该怎么改world = input()
world01 = world.split()
world02 = world.split()
text = input()
t = text.split()
i = 1
final = 0
while i<=len(t):
    if t == world01:
      final=final+1
      i+=1
    elif t==world02:
      final=final+1
      i+=1
    else:
      i+=1
    print(final)

      
      

yuedong 发表于 2021-3-5 22:52:12

world = input('请输入要检测的关键字:')
world01 = world.split()
world02 = world.split()
text = input('请输入要检测的语句:')
t = text.split()
i = 0
final = 0
while i < len(t):
    if t == world01:
      final = final + 1
      i += 1
    elif t == world02:
      final = final + 1
      i += 1
    else:
      i+=1
print('关键字出现的次数是:',final)

jtxs0000 发表于 2021-3-6 09:14:00

world = input()
world01 = world.split()
world02 = world.split()
text = input()
t = text.split()

result =sum(1 for i in range(len(t)) if t == world01 or t == world02)
# 如果上面的还没有学到就用下面的

# result = 0
# for i in range(len(t)):
#   if t == world01 or t == world02:
#         result += 1

print(result)

fishggg 发表于 2021-3-6 20:26:59

world = input("请输入要校验的单词:")
world01 = world.split()
world02 = world.split()
text = input("请输入待校验的文本:")
t = text.split()
i = 1
final = 0
if (world01 not in t) and (world02 not in t):
    print(final)#都不在里面就不要循环校验了
else:
    while i < len(t):
      final = final +1 if (t == world01) or (t == world02) else final
      i += 1
    print(final)
页: [1]
查看完整版本: 关于列表和循环