你好,我是python和编程的新手,我该如何将这些组合起来呢?
if "Web" in source:
source = "WEB"
if ((source == "Blu-ray") and (other == "Remux") and (reso == "1080p")):
reso = "BD Remux"
if "DVD" in name:
reso = "DVD Remux"
if ((source == "Ultra HD Blu-ray") and (other == "Remux") and (reso == "2160p")):
reso = "UHD Remux"
if source == "Ultra HD Blu-ray":
source = "Blu-ray"
您可以使用elif
子句扩展if
语句,并添加额外的条件:
mystring='what will it print?'
if mystring == 'hello':
print('world!')
elif mystring == 'good':
print('bye!')
elif mystring == 'how':
print('are you?')
else:
print('I ran out of ideas!')
[out]: I ran out of ideas!
稍微重写一下你的例子可以像下面这样:
source='Ultra HD Blu-ray'
name='DVD'
reso='2160p'
other='Remux'
resos={'1080p':'BD Remux','2160p':'UHD Remux'}
if "Web" in source:
source = "WEB"
elif "Blu-ray" in source and other == "Remux":
source = "Blu-ray"
reso = resos.get(reso,'UNDEFINED')
elif "DVD" in name:
reso = "DVD Remux"
print(source, name, reso)
[out]: Blu-ray DVD UHD Remux
请注意,我使用resos
字典来替换两个if
声明,这里有更多详细信息。
将这么多语句合并到一行(问题是一行吗?)可能不会是"python式的"。括号也是不必要的。
if "Web" in source:
source = "WEB"
elif source == "Blu-ray" and other == "Remux" and reso == "1080p":
reso = "BD Remux"
elif "DVD" in name:
reso = "DVD Remux"
elif source == "Ultra HD Blu-ray" and other == "Remux" and reso == "2160p":
reso = "UHD Remux"
elif source == "Ultra HD Blu-ray":
source = "Blu-ray"
else:
source = ""
reso = ""