Search code examples
pythonuser-inputdivision

Divisibility issue


while True:
    value_1 = raw_input (" Please Enter the price in total cents or type 'done' to exit: ") 
    if value_1 == "done" :    
        print " Thank you,Good-Bye!"
        break
    else:
        value_1 = int(value_1)
        if value_1 % 5 == 0:
            continue  
        else:
            print "\n  Please Re-enter the Price in multiples of 5, Thank you!"
    if value_1 % 100 == 2 :
           print "x"

check for screen shot enter image description here If I enter a number multiple of 5 it should continue to run but its going back again to top or say if I enter 200 it should suppose to print x but its doing nothing its prompting again for user input


Solution

  • So, as you may have found out, the "continue" command doesn't make your code continue to the next step, but rather, it brings it back to the start of the loop. Thus, your code, if you enter a number that is divisible by 5, brings you back to the start of the loop and doesn't implement the second check.

    The second problem is that, if I understand correctly that you want the script to print "x" if you enter 200, is that the second check

    if value_1 % 100 == 2 :
    

    checks if the remainder of the number if divided by 100 is 2. And, since before this check, you checked if the number was divisible by 5, you will never get the program to print "x". What you want is

    if value_1 / 100 == 2
    

    Additionally, to circumvent that "continue" problem, just nest the two checks like this

    if value_1 % 5 == 0:
        if value_1 / 100 == 2 :
            print "x"  
        else:
            print "\n  Please Re-enter the Price in multiples of 5, Thank you!"
    

    With this, if you type a multiple of 5, it brings you back to the prompt, and only if you type 200, it will print "x"