请教下for i in favourite为什么会单个输出字符?
for i in favourite这个条件是一直为真吗?如何判断的?favourite = "FishC"
for i in favourite:
print(i,end=' ')
weibo.com/ttarticle/p/show?id=2309405112663889018955
weibo.com/ttarticle/p/show?id=2309405112663863853236
weibo.com/ttarticle/p/show?id=2309405112663842619461
weibo.com/ttarticle/p/show?id=2309405112663838687648
weibo.com/ttarticle/p/show?id=2309405112663809065241
weibo.com/ttarticle/p/show?id=2309405112663792287931
weibo.com/ttarticle/p/show?id=2309405112663788356296
weibo.com/ttarticle/p/show?id=2309405112663754801339
weibo.com/ttarticle/p/show?id=2309405112663742218504
weibo.com/ttarticle/p/show?id=2309405112660822983540
weibo.com/ttarticle/p/show?id=2309405112660826914994
weibo.com/ttarticle/p/show?id=2309405112660776583463
weibo.com/ttarticle/p/show?id=2309405112660772651766
weibo.com/ttarticle/p/show?id=2309405112660726514434
weibo.com/ttarticle/p/show?id=2309405112660722320446
把favorite遍历一边,相当于一次拿一个字符,然后依次输出 for i in favourite 并不是一个条件语句,而是一个循环语句。它不是“真”或“假”的问题,而是用于遍历可迭代对象(如字符串、列表、元组等)中的每一个元素。
如果你想要检查一个特定的条件是否为真,你可以使用 if 语句。例如,如果你想检查 i 是否等于某个特定的字符,你可以这样做:
favourite = "FishC"
for i in favourite:
if i == 'C':# 条件判断
print("找到了'C'")
else:
print(i, end=' ')
页:
[1]