Using a While Loop with Conditions

Posted 13 November 2011 - 06:13 PM

The question is to ask the user for length and width inputs for a rectangle having both width and length be between [1,50]. If the user enters a number not between 1 and 50 the program should repeat the question and use the newly entered values (for further operations). This is what I have so far and the problem I am facing is that my program always stores the FIRST inputs as the variables for width and length

length = input('What would you like the length of the rectangle to be? ')
width = input('What would you like the width of the rectnagle to be? ')
int_len = int(length)
int_wid = int(width)
if 0 <= int_len <= 50 and 0 <= int_wid <= 50:
keepgoing = 'no'
else:
keepgoing = 'yes'
while keepgoing == 'yes':
length_w = input('What would you like the length of the rectangle to be? ')
width_w = input('What would you like the width of the rectnagle to be? ')
int_len_w = int(length_w)
int_wid_w = int(width_w)
if 0 <= int_len_w <= 50 and 0 <= int_wid_w <= 50:
keepgoing = 'no'
else:
keepgoing = 'yes'

int_len = -1
int_wid = -1
while not (0 <= int_len <= 50 and 0 <= int_wid <= 50): #notice using the condition for the loop
length = input('What would you like the length of the rectangle to be? ')
width = input('What would you like the width of the rectangle to be? ')
int_len = int(length)
int_wid = int(width)

and even better

length = -1
width = -1
while not (0 <= length <= 50 and 0 <= width <= 50):
length = int(input('What would you like the length of the rectangle to be? '))
width = int(input('What would you like the width of the rectangle to be? '))

Re: Using a While Loop with Conditions

while True:
leng = int(input('What would you like the length of the rectangle to be? '))
wid = int(input('What would you like the width of the rectangle to be? '))
if 0<=leng<=50 and 0<=wid<=50: break

This will do the trick with a minimum of code.

Not actually true. Both of our programs have 5 lines of code. You simply put the break statement on the same line as the condition. Your code could (and many people would argue should) be re-written to look like this:

while True:
leng = int(input('What would you like the length of the rectangle to be? '))
wid = int(input('What would you like the width of the rectangle to be? '))
if 0<=leng<=50 and 0<=wid<=50:
break

and this is still mine:

length = -1
width = -1
while not(0 <= length <= 50 and 0 <= width <= 50):
length = int(input('What would you like the length of the rectangle to be? '))
width = int(input('What would you like the width of the rectangle to be? '))

Very similar, but which one is better? Well, yours technically has to check the condition responsible for the while loop (i.e. while True) on each iteration of the loop as well as the conditional statement at the end. If there's only 1 iteration both programs go through 5 statements.
If there's 2 iterations, mine goes through 8 where as yours goes through 9.
At 3 iterations it becomes 11 to 13
At 4 iterations it becomes 14 to 17
At 5 iterations it becomes 17 to 21
At 6 iterations it becomes 20 to 25

Thus, we can observe that my program's order of growth is (i*3)+2 whereas yours is (i*4)+1 where i represents the number of iterations. If you look at the structure of the programs, you can see how these numbers make sense.

This post has been edited by atraub: 19 November 2011 - 02:12 AM
Reason for edit:: Added more math and a better explanation

Re: Using a While Loop with Conditions

Posted 19 November 2011 - 03:33 AM

I didn't say that only mine is the minimum, if yours is too, that's fine with me. I just didn't like the idea of defining variables before they are needed, that's sort of like C or C++ syntax, minus the strong typing. I wanted to define them on an ad-hoc basis, hence I gave another solution. By the way, yours happen to be the shortest one, for the first two lines can be included into a tuple-unpacking:

Re: Using a While Loop with Conditions

Posted 19 November 2011 - 06:58 AM

Programs, especially Python programs, shouldn't be judged on the minimum lines of code, lines of code doesn't equate to complexity. I am, and I'm sure many other people are, one for readable clean code, instead of it all bunched up on a single line. The execution time doesn't change in that respect.

I don't know if it's necessarily a syntactic thing, more a readability thing to have all your (global) variables defined at the top, I'd say it was good programming practice. I do tend to steer away from global variables if I can, but most of my variables end up at the beginning of functions.

All the programs have the slight annoyance of having to re-enter the length or the width if one of them, but not both of them, is correct. I'd probably go for, something like:

Re: Using a While Loop with Conditions

Posted 19 November 2011 - 10:17 AM

Couple of things:

First off, once again, putting 2 lines of code on one line doesn't make your program any shorter when it's interpreted. Simown is right, most programming shops have coding standards that dictate you define the variables at the beginning. This can substantially help with troubleshooting later on and at times can have a positive effect on performance.

Simown, I like that function you made. My only critique (and it's a minor one) is your choice of variable name 'type'. type is a keyword in Python. The word 'name' would be a better choice.

and it is another thing to declare 5 variables for your own use like this:

a,b,c,d,e = 1,2,3,4,5

Would you prefer the last one to this one:

a = 1
b = 2
c = 3
d = 4
e = 5

even though, like you said, they require the same amount of interpretation? If interpretation were the only thing, we would have always exchanged the values of two variables in the good old C way:

temp = a
a = b
b = temp

but then why do we use

a,b = b,a

?

If for the same amount of interpretation we can reduce the code (not convolute it, like accepting ten user inputs in one god-damned line), shouldn't we do it? And don't keep on harping that I started this less-line-less-code shebang. Look back the posts to find who did. I pointed out precisely why I have used the word 'minimum', that's because I did not want to introduce the variables before they were required and end up in assigning them values twice (the first assignment being useless, just to define the variables, which reminisces of the declaration syntax (minus the strong typing) in other languages Python is supposed to do away with). But as you pointed out, yours have a higher efficiency in spite of that, and yes, I can live with that.

Re: Using a While Loop with Conditions

I pointed out a flaw in both your logic and Simown's code. The difference is, Simown is an adult and when I mentioned his error, he simply acknowledged the error and moved on. By that same token, Simown offered an improvement on my initial code and it was a genuine improvement from a usability standpoint. I could have gotten butt-hurt and made stupid, nonsensical arguments to somehow discredit him, but instead, I told him that I like the improvement (admittedly, I pointed out a minor gotcha, but I even went out of my way to call it minor). Look at the way you've chosen to respond and I hope you try to reflect a little on it.

Firstly, like it or not, you claimed that your code was an improvement because you minimized the amount of code.

Quote

This will do the trick with a minimum of code.

However, your code was less efficient mathematically, less clear stylistically, and the same number of lines of code when properly formatted. To be honest, the discussion actually served as an excellent demonstration of calculating the order of growth of functions.

No one, at any point, claimed that utilizing python's syntax is bad. However, one should always strive for clarity. If you think that putting two assignment statements on the same line increases clarity, good for you.

You still seem to be acting on some sort of notion that less lines of code is better. If you define better as shorter, then you're right. Most of us define good code on a balance of clarity, efficiency, and usability.

Re: Using a While Loop with Conditions

Posted 19 November 2011 - 10:36 PM

Well, I have been saying in my last 3 posts, that I said it was minimum only because it didn't require variables to be defined beforehand, but only when required. I also admitted that yours had a better efficiency. Why shouldn't I? I just pointed out that when Python offers easier assignments, we can avail ourselves of that. Initially I didn't even compare the number of lines in my code and yours when I wrote the word 'minimum,, not until you brought it up in your reply. When you are constantly accusing me of something I never claimed, how do you expect me to remain calm?