0

Hi i'm new to python and programming, How would I go about combining these:

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"
Hani Umer
  • 21
  • 4

2 Answers2

1

You can use the elif clause to extend your if statement with extra conditions:

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!

A slight re-write of your example could look as follows:

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

Note that I've used the resos dictionary as a replacement to two if staments, more details about this here.

jdsalaro
  • 378
  • 2
  • 10
  • how would i combine these into one line?? – Hani Umer Jan 31 '21 at 05:23
  • What is your motivation for doing that? – jdsalaro Jan 31 '21 at 05:30
  • In that case I'd advise against doing that, especially when you're new to Python. Less lines of code are not always better than more LoC. Striving for shortness while not compromising on readability, correctness and debugging ease is not trivial. However, to answer your original question, it is possible to turn your code into one short, albeit unclear, line: `source, name, reso, other = 'WEB' if 'Web' in source else source, name, 'DVD Remux' if 'DVD' in name else {'1080p':'BD Remux','2160p':'UHD Remux'}.get(reso,'UNKNOWN') if other=='Remux' and "Blu-ray" in source else reso, other` – jdsalaro Jan 31 '21 at 14:38
0

Combining that many statements in a single line (was the question ONE line?) probably wouldn't be "Pythonic". The parentheses are also unnecessary.

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 = ""
DeFeNdog
  • 1,156
  • 1
  • 12
  • 25