Introduction to Python Programming Course - Part 9

 Still got a fair bit with Control Flow. But looking ahead, there's not much with the other sections e.g. functions. Going to do another half hour or so - that will be it for the weekend!

Start Time - 13:11

While Loops

For - definite iteration. Definitive number of times the body runs. 

Indefinite - this is while. A great distinction! Never got that before. So while loop ends when a condition is met.

Using sum as a method.  Then pop method too.

Checking the condition and running the loop until it becomes FALSE.

Value of test condition has to change otherwise it would be infinite!


Factorials - Practice

Awesome! Got it. All  logic and all worked!

Not sure about doing it with for loop - will find out...

Ah yes the range option. So it would be using that method to set the parameters. Forgot that to be fair.

Quiz on While loops..

Yes got this bit! Wasn't sure at first...

start_num = 4 #provide some start number

end_num = 12 #provide some end number that you stop when you hit

count_by = 2 #provide some number to count by 


# write a condition to check that end_num is larger than start_num before looping

break_num = start_num

if end_num > start_num:

# write a while loop that uses break_num as the ongoing number to 

    while break_num < end_num:

     break_num += count_by

     result = break_num

#   check against end_num

else:

    result = "Oops! Looks like your start value is greater than the end value. Please try again."


print(result)


Solution was a bit different but mine still worked fine - just didn't start with the error message.

Square number bit. Too tricky at the moment!

Something I've learned is you can declare new variables within the body of the control flow.


For vs While

For - better for finite iterations e.g. in lists, collections etc. A range - using that method - all declared. 

While - continue until condition is met - comparison operator e.g. x <= 50. While something is the same as a value e.g. user == "y".

For the challenge, I went for for loop but it was while - even though it was a list, it was only a certain number of iterations - only the first five relevant elements. Explicitly controlling the exit. OK!

My code worked though!!


num_list = [422, 136, 524, 85, 96, 719, 85, 92, 10, 17, 312, 542, 87, 23, 86, 191, 116, 35, 173, 45, 149, 59, 84, 69, 113, 166]

sum = 0

count = 0


for num in num_list:

    if num % 2 != 0 and count < 5:

        sum += num

        count += 1

        

print(sum)


Yeah I've tested it and it works. So I disagree on this - I used a list, was able to set the conditions within the for so it does work fine.

I DISAGREE WITH THE SOLUTION! That's good though right? And I prefer my code. It's neater and more elegant. Good to disagree!


Finish Time - 13:50 (approx 40 minutes).

That's all on While loops and For vs While. I preferred the for, especially as it was a definitive collection I was looking at. That did not add up and I'm proud that my code went well and I stuck to my guns! Interesting one. 

Comments