DEV Community

TheFoxlion
TheFoxlion

Posted on

Pseudocode/Python Project

Hey All.
I am writing a program that uses conditionals as well as a while loop.
I am running into problems with the while loop.
The loop is set to run if the user doesn't enter 'yes' or 'no' when asked whether they would like to make a payment. No matter where I try to place the loop it seems to get stuck if I enter anything other than 'yes' or 'no' then enter either 'yes' or 'no' after the loop.
It seems that if I try to catch someone not entering either of the two acceptable entries the program gets stuck.

def Credit_card():
    ccb=float(input("What is your credit card balance?\n"))
    pur=float(input("How much have you spent in purchases?\n"))
    minpymt=pur*.12
    print("Your minimum payment is ${0}".format(minpymt))
    dectopay=input("Would you like to make a payment?\n Please type 'yes' or 'no'\n")
    if (dectopay=="yes"):
        pymt=float(input("Wonderful! How much would you like to pay?\n"))
        while(pymt<minpymt):
          pymt=float(input("We're sorry, but your payment must be greater than or equal to your minimum payment of ${0}\nHow much would you like to pay?\n").format(minpymt))
        print("Thank you for your payment!\n")
        print("Your new credit card balance is ${0}\n".format((ccb+pur)-pymt))     
    elif(dectopay=="no"):
        print("Your credit card balance is ${0}. Thank you for visiting Credit Central. \n Have a wonderful day!".format(ccb+pur))
    while (dectopay!="yes")or(dectopay!="no"):
            dectopay=input("We're sorry. You must answer either 'yes' or 'no'\n")




Credit_card()


Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
thefoxlion profile image
TheFoxlion
def Credit_card():
    ccb=float(input("What is your credit card balance?\n"))
    pur=float(input("How much have you spent in purchases?\n"))
    minpymt=pur*.12
    print("Your minimum payment is ${0}".format(minpymt))    
    dectopay=input("Would you like to make a payment?\n Please type 'yes' or 'no'\n")
    while (dectopay!="yes")and(dectopay!="no"):
            dectopay=input("We're sorry. You must answer either 'yes' or 'no'\n")
            continue
    if(dectopay=="yes"):
        pymt=float(input("Wonderful! How much would you like to pay?\n"))
        while(pymt<minpymt):
            pymt=float(input("We're sorry, but your payment must be greater than or equal to your minimum payment of ${0}\nHow much would you like to pay?\n").format(minpymt))
        print("Thank you for your payment!\n")
        print("Your new credit card balance is ${0}\n".format((ccb+pur)-pymt))     
    if(dectopay=="no"):
            print("Your credit card balance is ${0}. Thank you for visiting Credit Central. \n Have a wonderful day!".format(ccb+pur))
Enter fullscreen mode Exit fullscreen mode

And now it works as I intended.

Thank you @sharavana and @harbingeroffire for your explicative responses.

I posted this on devstack overflow and a lead developer responded that the while loop is not doing what I think. This is correct and it did lead me to things like the try and except commands but to no avail. Your explanations highlighting the difference between using 'and' versus 'or' makes perfect sense. Because I was using 'or' the condition was always satisfied. It seems silly because what I wanted was 'and'. Or I suppose I meant neither/nor or 'yes.|.no'.

~(A^B) --> ~A^~B which is what I meant by specifying 'yes' or 'no' in the dectopay variable. Should the user have not entered 'yes' and not entered 'no' the loop should go.

Again, Thanks!

MB

Collapse
 
harbingeroffire profile image
HarbingerOfFire

The @sharavana gave was pretty much correct, but I'm going to expand on it a little. The or comparison looks to see if one of the supplied arguments is true. In your case the arguments are dectopay!="yes" and dectopay!="no". If a user enters "yes", then the equation will return true because dectopay!="no" has been satisfied ("yes"!="no"). Same going the other way. If "no" is entered, "no"!="yes" satisfying the first argument. Now if somebody enters "y" (neither yes or no), then we see 'y'!="yes". Therefore, al situations return true, and continue the loop. However, using the and comparison states that both arguments must be true (a is true and b is true). This way, it only returns false when "yes" or "no" are entered because one argument is satisfied but not the other. However, when they input a non-predicted value, they both return true, executing your line of code. Hopefully that explains it easily enough.