Skip to content Skip to sidebar Skip to footer

What's Going On With My If-else Statement? (python 3.3)

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 wit

Solution 1:

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 notin {"cking", "doneal", "mcook"}:
    print ("Invalid username. Please try again.")
    username = input ("Enter username:")
print ("Valid username.")

Solution 2:

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

ifusername== "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"orTrueorTrue:

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

ifusername== "cking"orusername== "doneal":

or simply do:

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

The same applies to password.

Solution 3:

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"~

Solution 4:

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.")

Post a Comment for "What's Going On With My If-else Statement? (python 3.3)"