#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    2

    Red face Im a noob with a super basic program


    so I haven't yet bought a book about python yet but I have been fooling around with it when I run this code however it says invalid syntax can someone please explain why

    here is the code




    # set changable value for the password

    password = " "

    # print a fancy line just for funzies

    print ("<@%@%$*^$#^^#%$^$^*$%@%@>")


    # prompt the user for input on the password

    input (" \n\n Welcome to practice enter password")

    # check if password is the set password for acess if it is then print "welcome"

    if password == "bob":
    then print ("hello")


    thanks ~ Nick
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    436
    Rep Power
    67
    Please use the CODE or HIGHLIGHT blocks for your code, otherwise the indentation is lost, and that’s a syntax error too.

    Anyway, there is no “then” in Python’s “if” structure.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    2
    Originally Posted by SuperOscar
    Please use the CODE or HIGHLIGHT blocks for your code, otherwise the indentation is lost, and that’s a syntax error too.

    Anyway, there is no “then” in Python’s “if” structure.
    ok thanks I did indent in my program but when I copied and pasted it did not come through
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    2

    my updated code


    so this is my updated code but now the program stops after I input the "password" bob instead of printing out my literal string




    # set changable value for the password

    password = " "

    # print a fancy line just for funzies

    print ("<@%@%$*^$#^^#%$^$^*$%@%@>")


    # prompt the user for input on the password

    input (" \n\n Welcome to practice enter password")

    # check if password is the set password for acess if it is then print "welcome"

    if password == "bob":
    print ("hello")
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    139
    Rep Power
    4

    set the variable


    it would seem to me that you would need to use "raw_input" rather than just the "input" command. also, the variable password is always just " " ( a space) because it is never being set to anything different, so your if statement will never be true. suggestion, replace "input" with "password = raw_input"
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    2
    thanks it worked
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    @WynnDeezl: In python3 raw_input is not available, and input works like raw_input did in earlier versions of python. That's why I avoid the issue and use sys.stdin . I didn't see a python version in Nick Wyden's posts.

    @Nick Wyden: highlight the program portion of your message, then click the # sign above the forum input editing space. Follow link at my signature for more information about space preservation in your posts.

    Finally, let's address the problem:
    your statement
    input (" \n\n Welcome to practice enter password")
    doesn't store the answer.
    Code:
    password = input (" \n\n Welcome to practice enter password")
    does store the response into password.
    If you're using python 3 the correct response is
    bob
    If you're using an earlier version of python you'd have to enter a string
    "bob"
    or you could follow WynnDeezl's advice to use raw_input
    or you could get it right in either python version and use

    import sys
    sys.stdout.write('prompt? ')
    password = sys.stdin.readline().strip()
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo