-2

Ok so, I am a noob at Python, to clear the air. I am 15 and just started learning Python, thought this is the best programming langauge to start with. This might seem very stupid, but here, who can debug this or autocorrect it? :P

def firstlogin(x,y):
    x=raw_input('Enter name: ')
    if x == 'Bob':
        return 'Hello there,press enter to go to the next test.'
    y=raw_input('Enter pass: ')
    if y == 'password':
        return ('You are now free to *** this account up')

since def is a function in which you can make your own functions, to prevent from writing the same functions over and over again, and or basically just make your own function. but I can't open this when I save it as .py. I think I have to input/import it into python when I want to use it for another project, or is there another way.

PS: self learning, I live in a country where I can't get programming language books any where :( .. so I surf the web.. use a little bit of Bucky's videos and etc.

Santosh Kumar
  • 26,475
  • 20
  • 67
  • 118
  • what do you mean by "I can't open this when I say it as .py " . Do you mean that when you execute the file , you don't get the required output? – misguided Jul 29 '13 at 04:31
  • 1
    I'd suggest starting with the official tutorial [here](http://docs.python.org/2/tutorial/). – l4mpi Jul 29 '13 at 07:11

3 Answers3

3

You can save this as any name you want, e.g. "foo.py".

Then in your other python file, you can use this function by importing it:

import foo
...

foo.firstlogin(bar,baz) # how to call the function.
jh314
  • 27,144
  • 16
  • 62
  • 82
2

This function won't do what you think, because you are returning after the first check, so it will just keep asking for the name and when the user enters Bob, nothing will happen because you aren't doing anything with the result.

Since you are overwriting whatever username and password you are passing in to the function, it will always check for Bob and not what you ask it to check for.

Try this version instead:

def firstlogin(user, passwd):
    ''' A function to check the username
        and password for a user '''

    x = raw_input('Enter Name: ')
    if x == user:
        y = raw_input('Great! Now try guessing the password: ')
        if y == passwd:
            print 'Correct!'
        else:
            print 'Sorry, password was wrong'
    else:
        print 'Sorry, username was wrong'

Save it to a file, lets say my_func.py, then in another file in the same directory, type:

from my_func import firstlogin

firstlogin('user','sekret')

Or you can type python to get to the interactive prompt, again from the same directory where you saved my_func.py:

>>> from my_func import firstlogin
>>> firstlogin('user','sekret')
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • I tried, everything.. but looks like it is totally wrong, it just ownt work :/ .. even when i f5 it , it just doesnt work xD – This Is Edis Mehmedovic Aug 05 '13 at 10:24
  • What do you mean "when I F5 it" - how are you running the code? – Burhan Khalid Aug 05 '13 at 10:27
  • Well instead of importing it, i would test it by running it on IDLE, that is what bucky did :P , and f5 is to start, when i start it nothing happens, not even an error.. i dont understand.. so the way I wrote this doesnt work at all :/ , atleast for me, even used the one you wrote up there, nada nothing.. just shows blank – This Is Edis Mehmedovic Aug 05 '13 at 14:57
1

One way to check out your function is shown below:

def firstlogin(x,y):
    x=raw_input('Enter name: ')
    if x == 'Bob':
        return 'Hello there,press enter to go to the next test.'
    y=raw_input('Enter pass: ')
    if y == 'password':
        return ('You are now free to *** this account up')


if __name__ == '__main__':
    firstlogin(bar, baz)

and then use this command: python foo.py

What is happening here is that the magic variable __name__ is being set by Python to the value __main__ to tell you that your module has been loaded directly from the command line.

When you invoke your module from another Python module as follows:

import foo
firstlogin(bar, baz)

the magic variable __name__ will be set to foo, the name of the module containing your function and therefore your bootstrapping code will not be executed. This is a standard idiom used in Python to allow you to checkout your code, even when you normally intend to invoke it from a different module. As you can see from this __name__ is normally the name of your module,but is set to __main__ when you invoke your module directly from the command line.

Jonathan
  • 2,635
  • 3
  • 30
  • 49