Fox
Fox

Reputation: 1

How to stop infinite loop?

Problem: Compute two integers based upon the user input that in the first is doubled repeatedly while second is divided by two. At each step, if second number is odd add current value of first number to itself until second number is zero.

My code doesn't seem to run completely, and I get an infinite loop what am I doing wrong? I'm using python 2.7.3

##
## ALGORITHM:
##     1. Get two whole numbers from user (numA and numB).
##     2. If user enters negative number for numB convert to positive.
##     3. Print numA and numB.
##     4. Check if numB is odd if True add numA to numA.& divide numB by 2 using int division.
##        Print statement showing new numA and numB values.
##     5. Repeat steps 3 and 4 until numB is 0 or negative value. enter code here
##     6. Prompt user to restart or terminate? y = restart n = terminate
##
## ERROR HANDLING:
##     None
##
## OTHER COMMENTS:
##     None
##################################################################

done = False
while not done:

    numA = input("Enter first integer: ") # 1. Get two whole numbers from user (A and B)
    numB = input("Enter second integer: ") # 1. Get two whole numbers from user (A and B)
    if numB < 0:
        abs(numB) # 2. If user enters negative number for B convert to positive
    print'A = ',+ numA,'     ','B = ',+ numB
    def isodd(numB):
        return numB & 1 and True or False
    while numB & 1 == True:
        print'B is odd, add',+numA,'to product to get',+numA,\
        'A = ',+ numA,'     ','B = ',+numB,\
        'A = ',+ numA+numA,'     ','B = ',+ numB//2
    else:
            print'result is positive',\
            'Final product: ', +numA

    input = raw_input("Would you like to Start over? Y/N : ")
    if input == "N":
        done = True

Upvotes: 0

Views: 5291

Answers (2)

npupp
npupp

Reputation: 1

Another problem here is that you are attempting adding to and dividing by numbers within in a print statement, so you do not change the values of the integers numA and numB at any point (that is, the integers numA and numB will remain constant throughout the program).

To change the variables numA and numB you must have:

  • variable name = (some function acting on variable)

e.g. numA = numA + 1 to add one to numA

Upvotes: 0

Katriel
Katriel

Reputation: 123632

Problems:

  • You don't need to write done = False; while not done:. Just loop infinitely (while True) and then use break to exit the loop when you're finished.

  • input executes the code that the user types (think of it like what the Python shell does). You want raw_input, which returns a string so you need to pass it to int:

    numA = int(raw_input("..."))
    
  • abs(numB) will compute the absolute value of numB, but won't do anything with it. You need to store the result of that function call in numB, which you do like numB = abs(numB).

  • The idiom x and True or False is not used in recent Python versions; instead, use True if x else false. However, returning True if x == True else False is the same as just returning x, so do that.

  • You don't need to loop while x == True. Just loop while x.

  • You never change the value of numB inside the inner loop, so it will never terminate.


Here's how I'd write it:

while True:
    A = int(raw_input("Enter first integer: "))
    B = int(raw_input("Enter second integer: "))
    if B < 0: B = abs(B)

    print 'A = {}, B = {}'.format(A, B)

    while B & 1:
        print 'B is odd, add {} to product to get {}'.format(A, A)
        A = # not sure what you're doing here
        B = # not sure what you're doing here
    else:
        print 'Finished: {}'.format(A)

    if raw_input("Would you like to Start over? Y/N : ").lower() == 'n':
        break

Upvotes: 2

Related Questions