Hacking to Win
Hacking to Win

Reputation: 55

How to fix "NameError: name 'checker_start' is not defined"

How to fix these error code

Traceback (most recent call last): File "/Users/erzajullian/PycharmProjects/Checker/topmail.py", line 9, in class checker_start(object): File "/Users/erzajullian/PycharmProjects/Checker/topmail.py", line 16, in checker_start print(checker_start().get_token()) NameError: name 'checker_start' is not defined

This is the code

import requests
from bs4 import BeautifulSoup


class output(object):
    pass


class checker_start(object):
    def get_token(self):
        data = requests.get("https://mail.topmail.com/preview/mail/")
        soup = BeautifulSoup(data.text, "lxml")
        token_1 = soup.find("input", {"name": "form_token"})["value"]
        return token_1

    print(checker_start().get_token())

What's wrong with my code?

Upvotes: 3

Views: 2185

Answers (2)

wuarmin
wuarmin

Reputation: 4015

Your line indentation of print(checker_start().get_token()) is wrong. You are trying to instantiate an object of class checker_start and call its method get_token in the code-block(scope) of the class-definition itself. Therefore you get a NameError.

One of the most distinctive features in Python are code blocks with their indentations. In Python it is not matter of style (as in the most programming languages) to indent your code, it is a requirement.

In most other programming languages, indentation is used only to help make the code look pretty. But in Python, it is required for indicating what block of code a statement belongs to.

Try:

import requests
from bs4 import BeautifulSoup


class output(object):
    pass


class checker_start(object):
    def get_token(self):
        data = requests.get("https://mail.topmail.com/preview/mail/")
        soup = BeautifulSoup(data.text, "lxml")
        token_1 = soup.find("input", {"name": "form_token"})["value"]
        return token_1

# remove the line-indentation
print(checker_start().get_token())

Upvotes: 3

brunns
brunns

Reputation: 2764

Your last line, print(checker_start().get_token()), is indented one level, and probably shouldn't be.

Upvotes: 1

Related Questions