WSQ07 – Funnier with numbahs

The pun in the name, yes I know, I had to do it, get over it.

So. Today’s practice obviously includes numbers… and also loops. If you’re keeping up with my posts, you should know about while loops, but now we’re deep into for loops. What are these? Well, if while loops go on until their condition isn’t met, for loops are kind of the opposite, they go on until their conditions are met… for example, if I want to print each letter in a string I’d do something like : 

————————————————————————–

for letter in “string” :

print letter

————————————————————————–

And the output would be :

————————————————————————–

s

t

r

i

n

g

————————————————————————–

Now that we’re all about for loops, let’s go on with the show. 

Our code should ask for two values, integers, and the output should be the sum of all the numbers between those two values, including them. For example, for the input 2 and 8, the code should make the operation 2 + 3 + 4 + 5 + 6 + 7 + 8 and return 35. 

That’s simple, right? The first thing that came to my mind was a scene about the School of rock movie, the second was this :

————————————————————————–

Editor :

Screen Shot 2016-02-13 at 4.35.24 PM

Terminal :

Screen Shot 2016-02-13 at 4.37.19 PM

————————————————————————–

What range does is exactly that, make a for loop run a certain number of times, the loop will run the number of times equivalent to the number of values between the first and the second input, and the value of i will be each and every one of those numbers, so our code easy does it, right?

So we’re done! Ok, cool.

 

 

 

 

 

 

 

 

But wait! What if the user inputs integers in an inverted order? for example,

Screen Shot 2016-02-13 at 4.40.18 PM
Screen Shot 2016-02-14 at 3.15.43 PM
Screen Shot 2016-02-14 at 3.39.29 PM
Screen Shot 2016-02-14 at 3.05.38 PM
Screen Shot 2016-02-14 at 3.54.48 PM

8 then 2… will range work? Well, then, let’s try that :

————————————————————————–

Terminal :

Screen Shot 2016-02-13 at 4.40.18 PM

————————————————————————–

Well, Houston, we’ve got a situation here. We’ll need something different now… Theres’s actually thousands of ways of fixing this. I see got two big options here, one could be making an error output for the user to see that is not valid and make them choose again. The other option is making the program work on both ways, which could be a bit more interesting…

Error output using while :

————————————————————————–

Editor :

Screen Shot 2016-02-14 at 3.15.43 PM

Terminal :

Screen Shot 2016-02-14 at 3.39.29 PM

————————————————————————–

Writing literally True as condition of a while loop makes it run nonstop, unless a break is met. In that case the loop stops, regardless of the kind of loop, either a for or a while. this is why we inserted the break inside the else, because when the if condition to print the error isn’t met, that means b is greater than a and the range outside of the while should be able to work perfectly.

What about making the code run both ways? That would go something like this using only conditionals :

————————————————————————–

Editor :

Screen Shot 2016-02-14 at 3.05.38 PM

Terminal :

Screen Shot 2016-02-14 at 3.54.48 PM

————————————————————————–

Well, those are two ways of dealing with that kind of situation. There’s tons of possibilities, most that can come to mind include lists, but I haven’t uploading anything on that topic yet, I’ll probably edit this entry and add another option after making a post on lists.

This post is for my #WSQ07 on the programming fundamentals course.

CC BY-SA 4.0 WSQ07 – Funnier with numbahs by esenombredeusua is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.