blackantt 发表于 2024-3-26 08:04:30

if ..elif 的条件调换一下结果就不对了,why?

本帖最后由 blackantt 于 2024-3-26 08:05 编辑


if transcript_list.find_manually_created_transcript(['en']):
      find_manually_created_transcript_en = transcript_list.find_manually_created_transcript(['en'])
      print(find_manually_created_transcript_en, type(find_manually_created_transcript_en),'==========================type1')
elif transcript_list.find_manually_created_transcript(['en-US']):
      find_manually_created_transcript_en = transcript_list.find_manually_created_transcript(['en-US'])
      print(find_manually_created_transcript_en, type(find_manually_created_transcript_en),'==========================type11')
elif transcript_list.find_generated_transcript(['en']):
      find_generated_transcript_en = transcript_list.find_generated_transcript(['en'])
      print(type(find_generated_transcript_en))
elif transcript_list.find_generated_transcript(['en-GB']):
      find_generated_transcript_en = transcript_list.find_generated_transcript(['en-GB'])
      print(type(find_generated_transcript_en))
      
else:
      print('-------------无字幕')
      continue
--------------------------------------------
ytbe_transcript_api._errors.NoTranscriptFound:
Could not retrieve a transcript for the video https://www.ye.com/watch?v=Qf6uZe4SDuY! This is most likely caused by:

No transcripts were found for any of the requested language codes: ['en']

For this video (Qf6uZe4SDuY) transcripts are available in the following languages:

(MANUALLY CREATED)
- en-US ("English (United States)")

(GENERATED)
- en ("English (auto-generated)")
-------------------------------------------------------
但把 transcript_list.find_manually_created_transcript(['en-US']) 放到if中, transcript_list.find_manually_created_transcript(['en']) 放到 elif 中就可以,为啥呢?

FishC 发表于 2024-3-26 08:04:42

在if...elif...else语句中,条件是按顺序逐个判断的。当满足某个条件时,就会执行对应的代码块,并退出整个if...elif...else结构。

在你的代码中,如果将`transcript_list.find_manually_created_transcript()`放在if中,而`transcript_list.find_generated_transcript()`放在elif中,那么只要`transcript_list.find_manually_created_transcript()`返回True,就会执行if代码块,并跳过elif中的判断。同样地,如果将`transcript_list.find_generated_transcript()`放在if中,而`transcript_list.find_manually_created_transcript()`放在elif中,只要`transcript_list.find_generated_transcript()`返回True,就会执行if代码块,并跳过elif中的判断。

因此,如果你希望两个条件都被判断到并执行对应的代码,应该将它们分别放在不同的if语句中,或者将它们合并成一个if语句,可以使用逻辑运算符`and`来连接两个条件。

下面是修改后的代码示例:

if transcript_list.find_manually_created_transcript():
    find_manually_created_transcript_en = transcript_list.find_manually_created_transcript()
    print(find_manually_created_transcript_en, type(find_manually_created_transcript_en),'==========================type1')

if transcript_list.find_generated_transcript():
    find_generated_transcript_en = transcript_list.find_generated_transcript()
    print(type(find_generated_transcript_en))

if not (transcript_list.find_manually_created_transcript() or transcript_list.find_generated_transcript()):
    print('-------------无字幕')
    continue

通过这样的修改,两个条件都会被判断到并执行对应的代码。另外,最后的else语句可以改为if not...来判断两个条件都不满足的情况。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页: [1]
查看完整版本: if ..elif 的条件调换一下结果就不对了,why?