已複製!

if vs. elif

發票對獎: 何種計算方式才正確?

          
num = '12345678'
money = 0
if num.endswith('12345678'):
    money += 200000
elif num.endswith('2345678'):
    money += 40000
elif num.endswith('345678'):
    money += 10000
elif num.endswith('45678'):
    money += 4000
elif num.endswith('5678'):
    money += 1000 
elif num.endswith('678'):
    money += 200
          
        
          
num = '12345678'
money = 0
if num.endswith('12345678'):
    money += 200000
if num.endswith('2345678'):
    money += 40000
if num.endswith('345678'):
    money += 10000
if num.endswith('45678'):
    money += 4000
if num.endswith('5678'):
    money += 1000 
if num.endswith('678'):
    money += 200