0

Good day I have the following snippet:

#! /usr/bin/python
#! /usr/bin/python3

from sys import version_info
import logging

py3 = version_info[0] > 2 

if py3:
    name = input('What is your name?: ')
    verify_user(name)
else:
    name = raw_input('What is your name?: ')
    verify_user(name)


def verify_user(name):
    if name is not 'My Admin':
        logging.warning('What the hell are you doing in here ???')
    else:
        logging.info('Welcome Dear Admin')

When I run it through the shell doing: ./log.py, I'm receiving the following error:

verify_user is not define. 

Not sure what is going on. I have defined the verify_user(). Any help will be really appreciated.

1
  • Python is an interpreted, dynamic programming language. Define before calling a function. Commented May 16, 2016 at 10:32

2 Answers 2

4

You're calling verify_user before you define it. Move the verify_user function to the top of your file.

#! /usr/bin/python 
#! /usr/bin/python3

from sys import version_info
import logging

def verify_user(name):
    if name is not 'My Admin':
        logging.warning('What the hell are you doing in here ???')
    else:
        logging.info('Welcome Dear Admin')

py3 = version_info[0] > 2 

if py3:
    name = input('What is your name?: ')
    verify_user(name)
else:
    name = raw_input('What is your name?: ')
    verify_user(name)
Sign up to request clarification or add additional context in comments.

1 Comment

when I run this snippet no matter what my keyboard input is, it's just showing the logging.warning(), while what I want is when the name input equals My Admin, to display the else part.
1

Python is a dynamic language, hence you need to 1st define the function and then use it.

Solution: Put the whole function definition before using it.

#! /usr/bin/python
#! /usr/bin/python3

from sys import version_info
import logging

def verify_user(name):
    if name is not 'My Admin':
        logging.warning('What the hell are you doing in here ???')
    else:
        logging.info('Welcome Dear Admin')

py3 = version_info[0] > 2 

if py3:
    name = input('What is your name?: ')
    verify_user(name)
else:
    name = raw_input('What is your name?: ')
    verify_user(name)

2 Comments

@ritesht93, when I run this snippet no matter what my keyboard input is, it's just showing the logging.warning(), while what I want is when the name input equals My Admin, to display the else part.
@thePhenom use != instead of is not

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.