if-else-then

Phil Frost indigo at bitglue.com
Fri Sep 10 19:42:55 EDT 2004


"=" and "==" are two different operators in Python. "=" is the asignment
operator; it asigns values to names. "==" is the equality operator; it
returns true if its operands are equal.

Also, to compare   name   to a string, the string must be in quotes.
Without quotes, python will look for a varable named "Stickie".

Furthermore, defining main won't make it run. A common idiom is to check
__name__ and run main. If the file is being run as a program, as opposed
to being imported as a module, __name__ will be "__main__".

Also, I don't see need for the "n" parameter, or the time.sleep(). I
have removed them.

Here is your program with these points applied:

#===========

def main():
    name = raw_input("Please enter your name: ")
    if name == "Stickie":
        print 'Yo masta', name
    else:
        print "Your name is", name

if __name__ == '__main__':
    main()

#===========

On Fri, Sep 10, 2004 at 04:34:58PM -0700, TuPLaD wrote:
> Hi, i got the following script:
> 
> name = raw_input("Please enter your name: ")
> print "Your name is", name
> 
> but i want it that way:
> 
> if 
> name = Stickie
> then
> print "You be di man"
> 
> how do i do it ?
> 
> This i what i have from a tutorial, but with syntax errors i dont know
> what im doing wrong :(
> 
> #!C:\Python23\python.exe
> # My first Python Application !
> # By TuPLaD
> # [email]spawnxx at pandora.be[/email]
> 
> import time
> def main(n):
>     name = raw_input("Please enter your name: ")
>     if name = Stickie:
>         print 'Yo masta', name
>     else:
>         print "Your name is", name
> 
> time.sleep(5)
> 
> what should i do ?



More information about the Python-list mailing list