标题:變數可以自動給空值嗎?
取消只看楼主
Z89
Rank: 2
等 级:论坛游民
威 望:1
帖 子:77
专家分:32
注 册:2022-4-26
结帖率:75%
 问题点数:0 回复次数:1 
變數可以自動給空值嗎?
請問有方法當變數d與e是空值時,自動省略  d='' , e=''

當沒指定變數時 強制給予一個空值,  變數=''

讓程序能繼續執行



程序代码:
a= 'trans'
b= 'relating'
c= 'cause '
#d='' #空值
#e='' #空值


y =  'yes '+a+' '+b+' '+c+' '+' '+d+' '+e
print(y)



[此贴子已经被作者于2022-6-20 15:19编辑过]

搜索更多相关主题的帖子: print 方法 空值 
2022-06-20 15:16
Z89
Rank: 2
等 级:论坛游民
威 望:1
帖 子:77
专家分:32
注 册:2022-4-26
得分:0 
可以耶~感謝!!!  如果是下面這樣開怎麼改?

程序代码:
import re

def Matches(sstr,html,a,b):    
    lens=re.findall(sstr,html)    
    if len(lens)==0 or a >= len(lens):
       return False
    else:
       if b =="":
           return lens[a]
       else:    
           tr =   lens[a]
           if b >= len(tr):
              return False
           else:
              return tr[b]
       

html='''

https://11111gov.tw/888.aspx

https://22222gov.tw/777.aspx

https://33333gov.tw/666.aspx

'''


bdd = Matches ('(https.+)tw/(.+)',html,0,1) #有b參數

print(bdd)



bdd = Matches ('(https.+)tw',html,0) #沒b參數  所以不填 
 
print(bdd)
2022-06-21 10:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-509392-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.693189 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved