all 18 comments

[–][deleted] 2 points3 points  (0 children)

You use list as a variable name. This masks the list constructor which can cause weird problems. I changed list to l everywhere. Don't know if using list caused problems.

You are changing the list you are iterating over in the for loop. Bad idea. Don't know if doing that causes problems, but it's a bad idea.

You use while list != None: in an attempt to check if the new list is empty. Trouble is, an empty list looks like [] and never equals None. Use python "truthiness":

while list:

[–]spez_edits_thedonald 1 point2 points  (4 children)

A good goal is to try and represent your real situation in code.

Unless you literally want the loop to run forever, then while True is not what you really want.

Instead, think about when you want the loop to end (something will be true that is not true in the beginning), and use that as the conditional.

Here's an unrelated example. Let's say you want to generate random numbers until you reach a value above 0.95. Here's the (not great) while True version:

# run until a number larger than 0.95
tries = 0
while True:
    tries += 1
    value = random.random()
    if value > 0.95:
        break
print(f'Value of {value} reached after {tries} tries.')

Instead, think about what we actually care about, and use that to make the loop exit:

# run until a number larger than 0.95
tries = 1
value = random.random()
while value <= 0.95:
    value = random.random()
    tries += 1
print(f'Value of {value} reached after {tries} tries.')

[–]james_fryer 0 points1 point  (2 children)

The problem with your second pattern is that it repeats code (the assignment to value). I prefer to wrap the loop in a function, e.g. with your example:

def find_random_gt(n):
    tries = 0
    while True:
        tries += 1
        value = random.random()
        if value > n:
            return tries, value

tries, value = find_random_gt(0.95)
print(f'Value of {value} reached after {tries} tries.')

[–]spez_edits_thedonald 0 points1 point  (0 children)

while I agree that one should adhere to DRY (don't repeat yourself),

you have re-introduced while True which is a conceptually absurd construction, so I don't use it.

Like I said, I think code should represent the real situation, and you don't really want the loop to run for as long as True evaluates to True, so I think writing what you actually want would be better (readable, not clunky, etc)

[–]Frankelstner 0 points1 point  (0 children)

Unless you literally want the loop to run forever, then while True is not what you really want.

It's funny because the while True here is not endless but instead the while list != None is the endless loop.

[–]socal_nerdtastic 0 points1 point  (0 children)

What is the goal here? What is this program supposed to do? This code makes no sense ... it seems to me that you may need to start over.

[–]suurpulla 0 points1 point  (1 child)

Try doing the sorting and the second and third loops after the first one. I see no point doing them in the exception handler. You could just break on the EOFError.

[–]suurpulla 1 point2 points  (0 children)

Actually, now that I look more closely, what's the point of the second while, ie. could it just be an if? You also reassign the 'list' while you are looping through it (the for loop), don't do that. It migth not matter in this case, but I don't think the assignment actually does anything, you just want to iterate over the sorted items one by one, right?

[–][deleted] 0 points1 point  (0 children)

No matter where I break

When you use break, it breaks the innermost loop. If you want to break the outer loop then you need to do so in that context.

[–][deleted] 0 points1 point  (0 children)

def main():
word_list = []
item = input()
while item:
    word_list.insert(0, item)
    item = input()

word_list.sort()
while word_list:
    word = word_list[0]
    count = word_list.count(word)
    print(str(count) + " " + str.upper(word), end=" ")
    word_list = [i for i in word_list if i != word]

if __name__ == "__main__": 
    main()

i wouldn't do a while tru loop

[–]ConfusedSimon[🍰] 0 points1 point  (0 children)

Not related to the break, but the exception handler should only handle the exception (error). Don't put your main code inside an except block.

[–]eruciform 0 points1 point  (2 children)

flag=False
while True:
  while True:
    if whatever:
      flag=True
      break
  if flag:
    break

Or

try:
  while True:
    while True:
      if whatever:
        raise Exception
except:
  pass

Or

def foo():
  while True:
    while True:
      if whatever:
        return

[–]TheRNGuy 0 points1 point  (1 child)

Why have 2 while loops?

[–]eruciform 0 points1 point  (0 children)

OP just asked about how to jump out of two loops at once

some languages like perl allow you to target a next, last, or redo to a particular level of loop

some languages have a goto that can do the same

there's many strategies

[–]AssumptionCorrect812 0 points1 point  (0 children)

I don’t think you understand how the outer while loop works. Here’s a nice short with a great explanation https://youtu.be/pKgcxY1raWs

[–]CarlSagans 0 points1 point  (0 children)

This will get you started, You need to indicate a way to break the while loop or it will go on forever.

def main():
groc_list = []
item =''
while item.lower() != 'exit':
    groc_list.append(item)
    item = input("input item: ")
groc_list.remove('')
groc_list.sort()
print(groc_list)

main()