0

Inside my code it has been skipping the for iteration Heres the code i have been using

 print ("part two")
varZ = varA - 0.5
varY = firstNumber / varZ

for varZ in list(reversed(range(firstNumber, 0))):
    print ("part three")
    varY = firstNumber / varZ
    if (varY - int(varY) == 0):
        print ("part four")
        break

    else:
        varZ = varZ - 1
        print ("part five")

print("Part six, Your answer is...", int(varZ))

Thanks for the help! P.s the output is

Your number sir? 27
Calculating...
13.5
part two
Part six, Your answer is... 13
1
  • please read this and this Commented Nov 7, 2015 at 18:20

2 Answers 2

1

range(firstNumber, 0) is almost certainly empty, unless you're expecting a negative firstNumber. It's unclear what you're trying to do here; if you're trying to iterate over something like [5,4,3,2,1,0], you should use range(5, 0, -1). Read the docs for more info

Sign up to request clarification or add additional context in comments.

2 Comments

It will be empty in any case where firstNumber is not >= 1. To generate an iterable that yields the numbers in descending order you also need to define the step: range(firstNumber, 0, -1)
yeah, it wasn't clear to me whether the poster was expecting a negative firstNumber, was confused about the range syntax and was trying to iterate in reverse with the reversed call, or whether he thought that switching the arguments in range would reverse the iterator or something. I'll maybe add some clarification to the answer
0

range(firstNumber, 0) is going to be an empty list (unless firstNumber is negative). The default increment is 1. If you get rid of the 0, then the range expression will count up from 0 to firstNumber-1. I'm not sure if you want to start with firstNumber or firstNumber-1, so I'll use n in this example:

for x in list(reversed(range(n))):

You can simplify this to:

for x in reversed(range(n)):

or just:

for x in range(n-1, -1, -1):

These will all count down from n-1 to 0, inclusive.

If you're using Python 2, you can use xrange in place of range to avoid actually constructing the list. In Python 3 you can just use range.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.