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.

the phenom
  • 45
  • 7

2 Answers2

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)
Anthony E
  • 11,072
  • 2
  • 24
  • 44
  • 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. – the phenom May 16 '16 at 10:48
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)
riteshtch
  • 8,629
  • 4
  • 25
  • 38
  • @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. – the phenom May 16 '16 at 10:49
  • @thePhenom use `!=` instead of `is not` – riteshtch May 16 '16 at 11:00