The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Y.G. Bae (36) [Avatar] Offline
#1
Due to the first if condition comparing against 5, I feel that the following print statement should be "x is greater than 5" in both versions. The proposed code follows as below:
### Version 1 of some code ###
x = 0
for x in range(100):
    if x > 5:
        print("x is greater than 5")
        if x%10 != 0:
            print("x is not divisible by 10")
            if x==2 or x == 4 or x==16 or x==32 or x==64:
                print("x is a power of 2")
                # perhaps more code

### Version 2 of some code ###
x = 0
for x in range(100):
    if x<= 5:
        continue
    print("x is greater than 5")
    if x%10 == 0:
        continue
    print("x is not divisible by 10")
    if x!=2 and x!=4 and x!=16 and x!=32 and x!=64:
        continue
    print("x is a power of 2")
    # perhaps more code


Subsequently, I feel that the following sentence after the listing should be:
Both versions of the code add up numbers between 0 and 100 that are greater than 5, not divisible by 10, and are perfect powers of 2.


However, to follow the explanation more closely, shouldn't the listed code be somewhat like below? I just initialized the variables in one line to save some space:
### Version 1 of some code ###
x = moret1 = ndiv10 = pow2 = 0
for x in range(100):
    if x > 1:
        moret1 += 1
        if x%10 != 0:
            ndiv10 += 1
            if x==2 or x == 4 or x==16 or x==32 or x==64:
                pow2 += 1
print("There are", moret1, "greater than 1s,")
print(ndiv10, "not divisible by 10s, and")
print(pow2, "power of 2s.")

### Version 2 of some code ###
x = moret1 = ndiv10 = pow2 = 0
for x in range(100):
    if x<= 1:
        continue
    moret1 += 1
    if x%10 == 0:
        continue
    ndiv10 += 1
    if x!=2 and x!=4 and x!=16 and x!=32 and x!=64:
        continue
    pow2 += 1
print("There are", moret1, "greater than 1s,")
print(ndiv10, "not divisible by 10s, and")
print(pow2, "power of 2s.")


If this listing is still work in progress, please disregard my comment. smilie
Ana Bell (33) [Avatar] Offline
#2
You are right, this listing was a bit odd and unfinished. I've modified it a bit and think I've gotten a better version. Thanks!