10 Replies - 3731 Views - Last Post: 19 November 2011 - 10:36 PM Rate Topic: -----

#1 collegecsstudent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-November 11

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'



Thanks for any help you can provide.

Is This A Good Question/Topic? 0
  • +

Replies To: Using a While Loop with Conditions

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Using a While Loop with Conditions

Posted 13 November 2011 - 11:50 PM

Here's a quick rewrite (your problem is that int_len_w and int_len are not the same, so your program likely uses the wrong one later on):

keepgoing = 'yes'

while keepgoing == 'yes':
    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)

    if 0 <= int_len <= 50 and 0 <= int_wid <= 50:
        keepgoing = 'no'
    else:
        keepgoing = 'yes'




here's a little more:

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? '))


This post has been edited by atraub: 19 November 2011 - 01:55 AM

Was This Post Helpful? 1
  • +
  • -

#3 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Using a While Loop with Conditions

Posted 18 November 2011 - 10:23 PM

You can try a while True loop:

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. :online2long:
Was This Post Helpful? 1
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Using a While Loop with Conditions

Posted 19 November 2011 - 01:39 AM

View Postcupidvogel, on 19 November 2011 - 01:23 AM, said:

You can try a while True loop:

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. :online2long:


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

Was This Post Helpful? 1
  • +
  • -

#5 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

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:

length, width = -1, -1;



which would reduce you code to 4 lines.

This post has been edited by cupidvogel: 19 November 2011 - 03:40 AM

Was This Post Helpful? 0
  • +
  • -

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

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:

def getValue(type):
    val = -1
    while not(0 <= val <= 50):
        val = int(input('What would you like the ' + type + ' of the rectangle to be? '))
    return val

length = getValue("length")
width = getValue("width")


Was This Post Helpful? 1
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

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.
Was This Post Helpful? 1
  • +
  • -

#8 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Using a While Loop with Conditions

Posted 19 November 2011 - 11:03 AM

Doh! I knew that! It was called "var" when I wrote the code, and changed it here to be a little more descriptive, but obviously slipped my mind - even though I suppose it's valid. Good catch :)

This post has been edited by Simown: 19 November 2011 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

#9 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Using a While Loop with Conditions

Posted 19 November 2011 - 11:30 AM

View Postatraub, on 19 November 2011 - 05:17 PM, said:

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.



I know that. But it's one thing to declare multiple variables from user inputs in one line like this:

a = input("Enter a: "); b = input("Enter b: "); c = input("Enter c: "); d = input("Enter d: ")



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.
Was This Post Helpful? -2
  • +
  • -

#10 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Using a While Loop with Conditions

Posted 19 November 2011 - 03:00 PM

Seriously? Are we really doing this? If you can't handle criticism on your code, don't bother offering it.

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.
Was This Post Helpful? 1
  • +
  • -

#11 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

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?

This post has been edited by cupidvogel: 19 November 2011 - 10:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1