0

I'm writing conditional statements for a billing program project. It's a bit advanced for a beginner I know but I welcome the challenge. Anyways, I plan on starting the program with asking for username and password. So this is my first coding for the program.

print ("Hello and welcome to Billing Pro, please enter your username and password to access the database.")

username = input ("Enter username:")

if username == "cking" or "doneal" or "mcook":
  print ("Valid username.")
else:
  print ("Invalid username. Please try again.") 


password = input ("Enter password:")

if password == "rammstein1" or "theory1" or "tupac1":
  print ("Valid password. User has been verified.")
else:
  print ("Invalid password. Access denied.")

Now at first when I ran this code if I had typed in anything other than the three choices for username Python printed out the 'invalid username' line. For some reason now, it's printing out 'valid username' and then going ahead with the password prompt. Also if I input anything other than the choices for passwords it will always read out the 'valid password' prompt.

Also how do I loop the username prompt when the user inputs something other than the three choices? Should I be using a while statement instead of if-else or can a while statement be placed at the end of the if-else statement to trigger the prompting again?

Oh and I know you can't tell because my crappy formatting in the question, but I did use proper indentation on the script itself.

kojiro
  • 74,557
  • 19
  • 143
  • 201
Courtney
  • 531
  • 1
  • 5
  • 8
  • possible duplicate of [Issues with a if/else loop in Python](http://stackoverflow.com/questions/13305089/issues-with-a-if-else-loop-in-python) – StephenTG Aug 20 '13 at 17:07
  • possible duplicate of [Using OR in Python for a yes/no?](http://stackoverflow.com/questions/16544393/using-or-in-python-for-a-yes-no) – abarnert Aug 20 '13 at 17:11
  • … and 100 more questions besides. Unfortunately, it's impossible to come up with a good search for this question, so it always gets re-asked… – abarnert Aug 20 '13 at 17:12

4 Answers4

5

The problem with your boolean expressions themselves is that they're always True.

if a == 'b' or 'c' is like if (True|False) or 'c', and since 'c' is truthy, it's True regardless of the first expression (a == 'b').

You either want a == 'b' and a == 'c'… or the more succinct a in {'b', 'c'…}, which checks if a is a member of the set.

If you want to loop, use a loop :)

while username not in {"cking", "doneal", "mcook"}:
    print ("Invalid username. Please try again.")
    username = input ("Enter username:")
print ("Valid username.")
kojiro
  • 74,557
  • 19
  • 143
  • 201
  • Thank you! I thought I had to use a loop but I didn't know how to make it a negative statement. – Courtney Aug 20 '13 at 17:09
  • 1
    Also, this loop is just going to print out an infinite number of lines as fast as possible. – abarnert Aug 20 '13 at 17:16
  • @abarnert: Are you saying that the loop won't reprompt? – Courtney Aug 20 '13 at 17:30
  • @cking: kojiro has since edited it, and the new version of course will, but [the previous versions](http://stackoverflow.com/revisions/18340974/2) would not. – abarnert Aug 20 '13 at 18:20
5

You need to compare your name with all names. Problem lies here:

if username == "cking" or "doneal" or "mcook":

Python evaluates first one to either true or false and then doing or with something, that evaluates to True in this context and at the end your comparison looks like this:

if username == "cking" or True or True:

which ends up being True. As suggested, you should use:

if username == "cking" or username == "doneal":

or simply do:

if username in ("cking", "doneal"):

The same applies to password.

Rapolas K.
  • 1,669
  • 26
  • 34
0

I think this code can help you

from sys import argv

    username=raw_input("Enter user name")
    password=raw_input("Enter password")

    if(username=="VARUN" or "Varun" or "varun"):
        print "\n\tvalid Username "
    else:
        print "\n\tUsername already existes"

    if (password=="@ppu1131986"):
        print "\n\tPasssowrd matched"
    else:
        print "\n\tWeak Password"
~                              
Varun Chhangani
  • 1,116
  • 3
  • 13
  • 18
0
print ("Hello, welcome to Facebook, please enter your username and password to access.")

username = input ("Enter username:")

if username == "cking":
  print ("Valid username.")
else:
  print ("Invalid username. Please try again.") 

  username = input ("Enter username:")

if username == "cking":
  print ("Valid username.")
else:
  print ("Invalid username. Please try again.") 


password = input ("Enter password:")

if password ==  "tupac1":
  print ("Valid password. User has been verified.")
else:
  print ("Invalid password. Access denied.")

  password = input ("Enter password:")

if password ==  "tupac1":
  print ("Valid password. User has been verified.")
else:
  print ("Invalid password. Access denied.")
n4m31ess_c0d3r
  • 3,028
  • 5
  • 26
  • 35