-3

When I run the program everything is fine until the password validity function is ran and all the conditions of length and uppercase and lowercase are equal to true. What the program is supposed to do here is run the creditcard function but it doesn't as seen here:

if length and upper and lower == True:
    creditcard()

It doesn't give me any errors or syntax messages about it when the conditions of length, upper, and lower case are all true but still will not run the creditcard function, the program simply ends. however when any of the conditions of length uppercase or lowercase are not met using or logic then "The password's invalid" is still successfully printed.

def creditcard():
    balence = input("What is your credit card balence this month")
    choice = input("Would you like to make a payment or make the standard deduction? y or n")
    intrest = .1596/12
    amountdue = balence*interest
    enter code here
    if choice == "y":
        payment = input("How much would you like to pay?")

    if payment == "n":
        if 0.3 * amountdue>25:
            payment = 0.3 * amountdue
        elif 0.3 * amountdue<25:
            payment = 25
    balence-=payment
    print("Your balence is now", balence)


def validity(password):

    length=()
    upper=()
    lower=()
    if len(password)<5:
        length = True
    else: lenghth = False
    for char in password:
        if char.isupper():
            upper = True
        else: upper = False
    for char in password:
        if char.islower():
            lower = True
        else: lower = False
    if length and upper and lower == True:
        creditcard()
    if length or upper or lower == False:
        print("The password's invalid")
password=""       
def main():
    password=""
    while password=="":
        password= input("Please enter a valid password")
        validity(password)`enter code here`
main()
deceze
  • 510,633
  • 85
  • 743
  • 889

1 Answers1

1

The problem is that your if condition is never being met. You can check this quickly by putting something like print("Going to call creditcard") between the if and creditcard() lines. You'll see the message is never printed. To find out why, you could print the values of length, upper and lower before the ifs to see if the values are what you expect.

That's how I'd recommend trying to diagnose your problem with quick and dirty debugging. As for explaining what your problem actually is:

When you do for char in password this is going through each character in the password and then running the test for is upper/lower for that character. This means if you have the password aB then on the first run it will check a and so will set lower to True, but then it will test the next character, B, and set lower to False. So basically, it's only checking the case of the last character, and as a single character can't be both upper and lower your condition for upper and lower being true will never be met.

A better way to do it is to initialise all the conditions as being false and then set them to true when met, e.g.

upper = False
for char in password:
  if char.isupper():
    upper = True

­ Edit: As deceze pointed out, you also need to be careful with the line if length or upper or lower == False. Because that is not the same as if (length == False) or (upper == False) or (lower == False). Instead, it evaluates it like: if length or upper or (lower == False), and an or operator returns true if either condition is true.

As an example, if the length = False, upper = False and lower = True, the condition would then be: if False or False or (True == False), which gets evaluated to False or False or False which gets evaluated to False. This may be a bit confusing, check the link deceze commented for a fuller description, but in your case a simpler way would be to just use an else.

There are a few more issues I can see, I realise you may not have got to this stage of checking your code yet, but just a heads up:

  • There are a few typos which can trip you up if you spell a variable differently throughout your code.

  • I assume that you want to length to be greater than 5? Currently you're using less than.

  • When you use input() the value is always a string (text) this will cause problems when you try to do your calculations with balance and payment. It's easy to overcome by telling Python to cast the data to a number by using int() for whole numbers, or float() for decimals, like so: balance = int(input("What is your credit card balance this month?"))

  • And finally (of the things that I can see), it looks like you're trying to make a loop so the user can keep re-entering the password if their previous attempt was invalid? But the condition on the while loop is just checking if the password is empty, so will terminate as soon as the user types in any password. You could create a new boolean called created_password initialised to False and use that as the condition? If I were you I'd do this by making validity just return True or False (depending on if the password is or isn't valid), then call creditcard() from main once out of the while loop.

I'd recommend you try to work through the errors yourself, this way you'll be able to actually see the error messages and learn how to fix them. If you get stuck I've made a version which works here: https://trinket.io/python3/b6b8c0ca31 which you can compare with your code.

Or a slightly more compact version: https://trinket.io/python3/d934a3a48e

ba5h
  • 97
  • 1
  • 11