Merhaba, bu şekilde bir yapı oluşturdum, ancak ‘search_words’ tanımlı değil şeklinde uyarı veriyor. Nerede hata yapıyorum kontrol etmenizi rica edebilir miyim? Nasıl düzeltebilirim?
NameError: name ‘search_words’ is not defined
try:
web_text = driver.find_element(By.XPATH, "/html/body/div[6]/div/div[2]/div[3]/div[1]")
words = ["Import","Abc", "Sw"]
search_words = [word for word in words if re.findall(word, web_text)]
text_words = ''
if search_words:
for i, word in enumerate(search_words):
if i < len(search_words) - 1:
text_words += f"{word}, "
else:
text_words += f"{word}."
ic(f"\nCannot send mail because it contains the word.Index : {text_words}")
ic(re.findall)
except Exception:
text_words = "Not Found Words"
ic(text_words)
time.sleep(1)
if (tel_number == 'Not Found Tel Number') and (search_words == 'Not Found Words'):
control = "true"
else:
control = "pass"
Aşağıdaki kod neden NameError yükseltiyorsa, sizin kodunuz da aynı nedenden ötürü NameError hatası yükseltiyor.
try:
x = 1 / 0
except ZeroDivisionError:
pass
print(x)
Burada, x'i try blokunda tanımladık. Ama 1 / 0 ifadesi ZeroDivisionError hatası yükseltecek. Yani try blokunda x tanımlanmaya çalışılacak ancak tanımlanamayacak ve except blokuna geçilecek, bu blokta da x'i tanımlamadığımıza göre, x diye bir değişkenimiz olmamış oluyor.
Değişkenlerin tanımlı olduğu scopelara dikkat etmeniz gerekiyor.
search_words değişkeni tanımlanmadan önce try bloğundaki kod çalışmayı durduruyor. Words değişkeninin tanımlanmasında bir sorun göremiyorum. Büyük ihtimalle web_text = … kısmında hata veriyor. try ın üstünde search_words değişkenini tanımlarsan hata düzelebilir. search_words = “” gibi boş değişken tanımlayabilirsin içine illaki bir şey yazmana da gerek yok.
Tanımladım, teşekkür ederim. Hata almıyorum ama o alan yapmak istediğim kontrolü yapıyor mu bilmiyorum. Test ettiğimde hep aynı sonucu getiriyor, ‘Not found words’.
try:
print("try çalıştı")
web_text = driver.find_element(By.XPATH, "/html/body/div[6]/div/div[2]/div[3]/div[1]")
words = ["Import","Abc", "Sw"]
search_words = [word for word in words if re.findall(word, web_text)]
text_words = ''
if search_words:
print("if çalıştı")
for i, word in enumerate(search_words):
if i < len(search_words) - 1:
text_words += f"{word}, "
else:
text_words += f"{word}."
ic(f"\nCannot send mail because it contains the word.Index : {text_words}")
ic(re.findall)
print("if tamamlandı")
print("try tamamlantı")
except Exception:
print("except çalıştı")
text_words = "Not Found Words"
ic(text_words)
time.sleep(1)