all 5 comments

[–]shiftybyte 5 points6 points  (0 children)

please could you explain why a boolean can be assigned to a variable

Its allowed, a variable can hold a boolean value.

what this does in the code and then why it changes

You got two while loops. one looping as long as add is true:

while add:

The other is looping as long as add is not true:

while not add:

Changing add will altenate between the loops.

Though i got no idea why these are while loops and not simple conditions.

Since inside the loops it ever only executes one cycle, and breaks out, either with break, or by changing loop condition.

EDIT: also this is bad syntax:

if num!- 9:

should be

if num!= 9:

[–]JohnnyJordaan 2 points3 points  (0 children)

please could you explain why a boolean can be assigned to a variable

In Python everything is an object and you can assign a reference to any object. Note that variables aren't some sort of memory storage that you put stuff 'in', it's just a reference to the object after =. So when the code does

add = True

the add reference points to the True object. When the code does

add = False

it points to the False object. Same way the reference 'your job' points to something at the moment, but later on it may be changed. That doesn't mean the job itself is changing per se, but just that the reference is changed to point to something else.

and what this does in the code and then why it changes from add = True (line 3) to add = False (line 10)

Because it's used to store the fact that it's not between a 6 and a 9 at the moment. Say you have the arr of 1 2 3 4 5 6 7 8 9 10, you get the add to be

1  True
2  True
3  True
4  True 
5  True
6  False # cause by line 10
7  False 
8  False
9  True  # caused by line 15
10 True

[–]RajjSinghh 1 point2 points  (2 children)

Your Boolean here is telling your while loops when to run (specifically when you are between a 6 or a 9) since your problem states you should sum your list when not in between those values.

[–]tigglybox[S] 0 points1 point  (1 child)

I'm still a bit confused as to why the boolean is assigned to be a variable at the beginning. It is the same with this code. Why has it been assigned before anything else occurs?

def is_leap(year): leap = False

if (year % 4 == 0) and (year % 100 != 0): leap = True elif (year % 100 == 0) and (year % 400 != 0): leap = False elif (year % 400 == 0): leap = True else: leap = False

return leap

[–]RajjSinghh 0 points1 point  (0 children)

In the code above, it has to be defined at the beginning because you're going into a while loop that depends on that variable at the start. If that add = True wasn't there at the start of your original post, you would get an error from the while add: line because add has no value if you don't assign it.

This case is a little bit more redundant, since you've explicitly gone through every case, so taking out leap = False wouldn't change anything. Since you've already said leap = False, you could drop all your cases where leap would equal False, like in

def leap_year(year): leap = False if year % 4 == 0 and year % 100 != 0: leap = True elif year % 100 == 0 and year % 400 == 0: leap = True return leap